SORM 持久化 'id' 未定义
Posted
技术标签:
【中文标题】SORM 持久化 \'id\' 未定义【英文标题】:SORM Persisted 'id' undefinedSORM 持久化 'id' 未定义 【发布时间】:2013-11-16 23:10:09 【问题描述】:我只是在尝试学习 SORM,并且正在使用我认为是一些简单的示例代码。对白衣:
case class Book(var author:String, var title:String);
object Db extends Instance(
entities = Set(Entity[Book]()),
url = "jdbc:h2:mem:test",
user = "",
password = "",
initMode = InitMode.Create
)
然后:
val b : Book with Persisted = Db.save( Book("foo","bar") )
尝试编译时,我得到:
[error] /Users/rjf/IdeaProjects/PlayTest/app/controllers/Application.scala:22: type mismatch;
[error] found : models.Book
[error] required: sorm.Persisted with models.Book
[error] val b : Book with Persisted = Db.save( Book("foo","bar") )
[error] ^
如果我将 Book 声明更改为:
case class Book(var author:String, var title:String) extends Persisted;
然后我得到:
[error] /Users/rjf/IdeaProjects/PlayTest/app/models/Book.scala:17: class Book needs to be abstract, since:
[error] it has 2 unimplemented members.
[error] /** As seen from class Book, the missing signatures are as follows.
[error] * For convenience, these are usable as stub implementations.
[error] */
[error] def id: Long = ???
[error] def mixoutPersisted[T]: (Long, T) = ???
[error] case class Book(var author:String, var title:String) extends Persisted;
[error] ^
为新手问题道歉。毫无疑问,我同时学习 Scala 是一个促成因素。
【问题讨论】:
【参考方案1】:要解决您的问题,只需删除显式类型注释:
val b = Db.save( Book("foo","bar") )
别担心,您仍然可以访问与Book with Persisted
相同的界面。
至于为什么会这样,这似乎是Scala的一个bug,it's been reported。
附注
不要在case类声明中使用var
s。不变性是案例类的重点。正确的声明是:
case class Book(author:String, title:String)
与
相同case class Book(val author:String, val title:String)
【讨论】:
谢谢 Nikita,解决了我的问题。我很欣赏我的案例课程中关于 var vs val 的注释,这绝对是我的疏忽。请注意,我在存储新实体 (sorm-framework.org/Documentation.html) 下复制了 SORM 文档中的原始声明。以上是关于SORM 持久化 'id' 未定义的主要内容,如果未能解决你的问题,请参考以下文章
会话 ID 未显示错误消息注意:未定义索引:用户 ID [重复]