Playback Models 2.0

I am looking for best practices regarding models and methods for storing objects in a database using game 2.0. I studied playback patterns and types for 2.0 playback using scala.

I understand:

  • The model is defined in the case class
  • All insertions / updates / deletes / selections are defined in the companion object of the case class

So, if I need to update my Car object to determine the new owner, I will need to do:

val updatedCar = myCar.copy(owner=newOwner)
Car.update(updatedCar)
// or
Car.updateOwner(myCar.id.get, newOwner)

I am wondering why update or delete statements do not apply to case class:

case class Car(id: Pk[Long] = NotAssigned, owner: String) {
    def updateOwner(newOwner: String) {
        DB.withConnection { implicit connection =>
            SQL(
                """
                update car
                set owner = {newOwner}
                where id = {id}
                """
            ).on(
                'id -> id,
                'newOwner -> newOwner
            ).executeUpdate()
        }
        copy(owner = newOwner)
    }
}

This will allow:

val updatedCar = myCar.updateOwner(newOwner)

This is what I used with Play 1.X, using Java and JPA. Perhaps the reason is obvious because of my little knowledge of scala.

+5
source share
1 answer

, - , Scala.

"this.owner". , "this"?

, ( ) , - .

, , , . , , , , ?

play2.0 , :

MyObject SalatDAO [MyObject, ObjectId] ( = getCollection ( "objectcollection" )) { }

- , CRUD, SalatDAO (MyObject.save(), MyObject.find() ..). , , .

+4

All Articles