从 squeryl 上的关系中选择

发布于 2024-10-06 11:17:10 字数 1517 浏览 1 评论 0原文

我正在测试(尝试)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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

战皆罪 2024-10-13 11:17:10

问题出在where子句上:
where(b.bfoo.size gt 0)

b.bfoo.size 导致 bfoo 查询隐式转换为 Iterable
(b.bfoo 是一个 OneToMany[Foo],它也是一个 Query[Foo],调用
.size 会导致查询的评估)。

您需要像这样重写“findBars”:

def findBars =

  from(bars)(b =>
    where(b.id in
              from(foo)(f=> where(f.foBar === b.id) select(f.foBar))
    )
    select(b)
  )

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 :

def findBars =

  from(bars)(b =>
    where(b.id in
              from(foo)(f=> where(f.foBar === b.id) select(f.foBar))
    )
    select(b)
  )

丢了幸福的猪 2024-10-13 11:17:10

您使用的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文