Request Grails criteria with two-level fetchMode

In my Grails project, I have the following classes:

class A {
    static hasMany = [cs:C]
}

class B {
    static hasMany = [cs:C]
}

class C {
    static belongsTo = [a:A, b:B]
}

I would like to request class A and look forward to revitalizing all associations from B and C. I tried the following criteria request, but when I iterate over Cs from A, hibernate uses lazy initialization to query for objects B.

A.withCriteria() {
    fetchmode "cs", FetchMode.JOIN
    fetchMode "cs.b", FetchMode.JOIN
}

Any ideas?

+5
source share
1 answer

solvable.

A.withCriteria() {
    cs{
        fetchMode "cs.b", FetchMode.JOIN
    }
}

or

A.withCriteria() {
    cs{
        b{
        }
    }
}

In both cases, Hibernate uses two queries. Much better than mine before :-)

+8
source

All Articles