Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 索姆坚持';id';未定义_Scala_Sorm - Fatal编程技术网

Scala 索姆坚持';id';未定义

Scala 索姆坚持';id';未定义,scala,sorm,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 :

我只是想学习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是一个促成因素。

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

val b = Db.save( Book("foo","bar") )
别担心,您仍然可以使用持久化的访问与
图书相同的界面

至于为什么会发生这种情况,这似乎是Scala的一个bug,而且

旁注 在case类声明中不要使用
var
s。不变性是case类的全部要点。正确的声明是:

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

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

谢谢你,尼基塔,这解决了我的问题。我很欣赏案例课上关于var vs val的注释,这绝对是我的疏忽。注意,我从SORM文档中复制了我的原始声明,在存储新实体()下。