4

我将playframework 2.2.1scala 2.10SORM 0.3.10用于 mysql db。当我试图保存简单案例类的实例时:

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

我遇到了这个错误:

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

我做错了什么?UserAuthToken也很简单:

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

这两个类都注册为 Db 对象中的实体。

4

1 回答 1

4

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) )
于 2013-11-29T14:03:38.473 回答