I have a 1 to many relationship where each group has multiple positions, i.e.
Group -1----N- Position
class Group { static hasMany = [postions: Position }
If I have a specific group instance, can’t I count directly how many positions it has? I.e. call some method group.positions.??
Thanks
As OverZealous said you can call
group.positions.size()but it will be very expensive for a large number ofPositions since it will load all of them into memory just to count them, and then throw them away.If your
Positionclass has a back-reference to its owningGroupclass, e.g.static belongsTo = [group: Group], then you can use this lightweight query:If you don’t have a bidirectional relationship, you can get the count via HQL: