Grails: Is there a way to get the _idx property of an object in a list?

I have such an association

class Parent List children static hasMany =[children:Child] 

I need to know the order of the child when I look at it outside the context of the parent. So that I can determine if this is the 1st child, the second child, etc.

+6
source share
1 answer

What do you mean by โ€œparental context limitsโ€? To get the index of an object in the list, you need to download it earlier. If you want to get an index without loading Parent, then you need to use raw SQL for this, but I'm not sure that it will be faster than loading Parent, because it is the same logic, except that the display of results for the model is available.

Btw, to get this index, when you have a Parent instance, you should use:

 int idx = parent.children.indexOf(child) 

And don't forget to implement the .equals your Child domain.

+3
source

Source: https://habr.com/ru/post/898874/


All Articles