从 squeryl 上的关系中选择
我正在测试(尝试)squeryl 的关系建模功能
class Foo(val id: Long, val foBar: Long) extends KeyedEntity[Long]{
//Many Foo's can have one Bar.
lazy val fbar: ManyToOne[Bar] = myschema.barToFoo.right(this)
}
class Bar(val id: Long) extends KeyedEntity[Long]{
//One Bar can be assigned to many Foo's.
lazy val bfoo: OneToMany[Foo] = myschema.barToFoo.left(this)
}
object myschema extends Schema{
val bars= table[Bar]
val foo= table[Foo]
val barToFoo =
oneToManyRelation(Bar, Foo).
via((b,f) => b.id === f.foBar)
/** Find all Bars that are assigned to at least one foo.*/
def findBars() ={
from(bars)((b) => where(b.bfoo.size gt 0) select(b))
}
}
如果我尝试使用以下代码测试该代码:
test("Test findBars"){
using(jdbcSession){
val mybars = telemetria.findBars
for{i <- mybars}{
println(i.id)
i.id should equal(1)
}
}
}
我收到以下错误:
java.util.NoSuchElementException: None.get
findBars 的主体抛出异常。我想不出有什么可能导致这样的问题。有人遇到过类似的情况吗?
由于 Daniel 指出这可能是一个编译问题,因此我附加了 build.properties 和来自 project.scala 的属性
project.organization=org.simepar
project.name=scalatra-sbt-prototype
sbt.version=0.7.4
project.version=2.0.0.M2
def.scala.version=2.8.1
build.scala.versions=2.8.1
project.initialize=false
;
val squeryl = "org.squeryl" % "squeryl_2.8.0" % "0.9.4-RC3"
I am testing(trying) squeryl's relations modeling feature
class Foo(val id: Long, val foBar: Long) extends KeyedEntity[Long]{
//Many Foo's can have one Bar.
lazy val fbar: ManyToOne[Bar] = myschema.barToFoo.right(this)
}
class Bar(val id: Long) extends KeyedEntity[Long]{
//One Bar can be assigned to many Foo's.
lazy val bfoo: OneToMany[Foo] = myschema.barToFoo.left(this)
}
object myschema extends Schema{
val bars= table[Bar]
val foo= table[Foo]
val barToFoo =
oneToManyRelation(Bar, Foo).
via((b,f) => b.id === f.foBar)
/** Find all Bars that are assigned to at least one foo.*/
def findBars() ={
from(bars)((b) => where(b.bfoo.size gt 0) select(b))
}
}
If I try to test that code with the following code:
test("Test findBars"){
using(jdbcSession){
val mybars = telemetria.findBars
for{i <- mybars}{
println(i.id)
i.id should equal(1)
}
}
}
And I get the following error:
java.util.NoSuchElementException: None.get
The exception is being thrown from the body of findBars. I can't think about anything that could be causing such problem. Have anyone run into a similar situation?
Since Daniel pointed out that it could be a compiling problem, I am appending the build.properties and a property from project.scala
project.organization=org.simepar
project.name=scalatra-sbt-prototype
sbt.version=0.7.4
project.version=2.0.0.M2
def.scala.version=2.8.1
build.scala.versions=2.8.1
project.initialize=false
;
val squeryl = "org.squeryl" % "squeryl_2.8.0" % "0.9.4-RC3"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题出在where子句上:
where(b.bfoo.size gt 0)
b.bfoo.size 导致 bfoo 查询隐式转换为 Iterable
(b.bfoo 是一个 OneToMany[Foo],它也是一个 Query[Foo],调用
.size 会导致查询的评估)。
您需要像这样重写“findBars”:
The problem lies in the where clause :
where(b.bfoo.size gt 0)
b.bfoo.size causes an implicit conversion of the bfoo Query to an Iterable
(b.bfoo is a OneToMany[Foo] which is also a Query[Foo], calling
.size on it cause the evaluation of the query).
You need to rewrite "findBars" like this :
您使用的 Squeryl 库可能是用不同版本的 Scala 编译的,而不是您用来编译自己的代码的版本。
一般来说,您的所有代码和库都需要由相同的 Scala 版本编译。据我所知,唯一的例外是 Scala 2.8.0 /2.8.1。
You are probably using an Squeryl library compiled with a different version of Scala than the one you are using to compile your own code.
Generally speaking, all your code and libraries need to be compiled by the same Scala version. The only exception to that, as far as I know, is Scala 2.8.0 /2.8.1.