SORM: Attempt to refer to an unpersisted entity

237 Views Asked by At

I'm using playframework 2.2.1 with scala 2.10 and SORM 0.3.10 for mysql db. When I'm trying to save instance of simple case class:

case class User(email: String, password: String, token: String, verified: Boolean = false, atoken: UserAuthToken) {
    def save = Db.save[User](this)
}

I'm gettin this error:

sorm.core.SormException: Attempt to refer to an unpersisted entity: UserAuthToken(7779235c1fd045f39ced7674a45baaa2,1387039847)

What I'm doing wrong? UserAuthToken is quite simple too:

case class UserAuthToken(token: String = UUID.randomUUID().toString.replace("-",""), expire: Int = (Calendar.getInstance().getTimeInMillis/1000).toInt + 60*60*365)

Both classes are registered as entities in Db object.

1

There are 1 best solutions below

1
On BEST ANSWER

UserAuthToken is an entity, meaning that it is mapped to some row in DB. For both you and SORM to be able to identify that row (and the entity), the Db.save(..) method returns a value of type UserAuthToken with Persisted, i.e. a copy of the original value with the identification information.

User is an entity too, but it refers to UserAuthToken, meaning that the row it is mapped to must store the identification info on UserAuthToken. So for you to be able to persist a value of type User, it must refer only to an already persisted UserAuthToken. I.e.:

..
val persistedUserAuthToken = Db.save(userAuthToken)
val persistedUser = Db.save( User(.., atoken = persistedUserAuthToken) )