Jils
Jils

Reputation: 783

Use create criteria on multiple associations

What is the most efficient solution to get the status attribut value from a ClassD instance by having its name attribut value and starting from the ClassA instance?

I can use on loop in each list, but I think there is a better solution by using a create criteria

Class ClassA {
    static hasMany = [CLassBList:ClassB]
}


Class ClassB {
   static hasMany = [CLassCList:ClassC]
   static belongsTo = [ClassA]
}


Class ClassC {
    static hasMany = [CLassDList:ClassD]
    static belongsTo = [ClassB]
 }


Class ClassD {
    String name
    String status
    static belongsTo = [ClassC]
}

Upvotes: 0

Views: 149

Answers (1)

shuttsy
shuttsy

Reputation: 1645

Am assuming (although your code doesn't mention it) that you have some criteria on ClassA that you want to search on, then you can use a createCriteria like this:

def c = ClassD.createCriteria()
def results = c.list () {
  projections { property('status') }
  classC {
    classB {
      classA {
        eq('classAProperty', someClassAValue)
      }
    }
  }
}

Use .get() rather than .list() if you expect only 1 result, then you might also want an order() clause.

Remove the projections if you want the entire ClassD returned, then deal with as you see fit.

Upvotes: 1

Related Questions