SORM坚持'id'未定义

时间:2013-11-16 23:10:09

标签: scala sorm

我只是想学习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]                                                ^

如果我将图书声明更改为:

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 个答案:

答案 0 :(得分:1)

要解决您的问题,只需删除显式类型注释:

val b = Db.save( Book("foo","bar") )

别担心,您仍然可以访问与Book with Persisted相同的界面。

至于为什么会发生这种情况,这似乎是Scala的错误,it's been reported

附注

不要在案例类声明中使用var。不变性是案例类的重点。正确的声明是:

case class Book(author:String, title:String)

相同
case class Book(val author:String, val title:String)