开发者

Selecting from relations on squeryl

开发者 https://www.devze.com 2023-01-29 13:06 出处:网络
I am testing(trying) squeryl\'s relations modeling feature class Foo(val id: Long, val foBar: Long) extends KeyedEntity[Long]{

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 anyo开发者_如何学运维ne 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"


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)
  )


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消