I have an association like this
class Parent
List children
static hasMany =[children:Child]
I need to be able to know the order of a Child object when I look at it outside of Parent context. So that I can tell if it is a 1st child, second child, etc.
What do you mean with 'outside of Parent context'? To get index of a object in a list - you need to load it before. If you want to get index without loading Parent, then you have to use an raw SQL for this, but i'm not sure that it will be faster than loading Parent, because it's the same logic, except making mapping resultset to a model.
Btw, to get this index when you have Parent
instance, you have to use:
int idx = parent.children.indexOf(child)
And don't forget to implement .equals
of your Child
domain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With