Question

Hello I am fairly new to Scala.
I want to learn Scala because:

  1. To get rid of boiler plate code from Java.
  2. Play with different paradigms.
  3. Just for fun of learning new things

I am following a tutorial on plays website.

Here is the tutorial code:

Player.create(Player(Id(1), "bob@gmail.com", "secret", "Bob", false)) 
Post.create(Post(NotAssigned, "My 1st post", "Hello world", new Date, 1))

To get deeper knowledge I played around and modified the examples.
I decided not to give the Player a predefined ID but let MySql assign it.
In MySql I have defined the Player id as an int but Post id as an long. // I will change this later but for this example it is important.

But what shocked me is the way I did it, look at the second row. This is not the elegant at all.

val player = Player.create(Player(NotAssigned, "bob@gmail.com", "secret", "Bob", false)) 
val id = player.get.id.get.get.asInstanceOf[Number].intValue()
Post.create(Post(NotAssigned, "My 1st post", "Hello world", new Date, id)

Now to my question how do I get the Id as an int from the created player in a less absurd way?

Was it helpful?

Solution

Not sure how much energy I would invest in NORM.

Play has been brought into the TypeSafe stack; as such, they are not so subtly promoting ScalaQuery as the SQL wrapper of choice in Play 2.0: https://github.com/playframework/Play20/wiki/ScalaDatabase

NORM feels like a stop gap measure of sorts, it has some nice features, avoids overhead of a full blown ORM like Hibernate, and is better than straight JDBC. It is not at all type safe, however; therefore, you can bet TypeSafe's ScalaQuery will be the Play standard (by Q3 at the latest, as Scala 2.10 [with reflection!] and ScalaQuery's morph into SLICK/SIQ [Google it] are coming)

I'm using ScalaQuery master branch now (you have to build it yourself, pretty straightforward with SBT), highly impressed.

Or, use NORM with Play that could work as well. Neither are my cup o' tea...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top