Search code examples
grailsgrails-orm

Grails: Children of Children


I have a question about GORM and "multiple" hasmany relationships, and I didn't find an answer in my previous searches.

Let's say we have three domains:

class A {
...
static hasMany = [Bs: B]
}

and

class B {
...
static belongsTo = A
static hasMany = [Cs: C]
}

and

class C {
static belongsTo = B
String name
dateCreated date
}

I want to know if it is possible to get a list of objects of the class C, sorted by dateCreated, using an object of the class A (something like C.findAll(...., a: a.id) ) or if I have to use a more complex query ?

Best regards,


Solution

  • Its a little more difficult because you aren't storing a back reference to the parent objects

    Something like this - I didn't test it myself

    A.executeQuery("select distinct c from A a join a.bs as b join b.cs as c where a = :a", [a: a])