开发者

Grails: Is there a way to retrieve _idx property of an Object in a List?

开发者 https://www.devze.com 2023-04-11 08:11 出处:网络
I have 开发者_如何学Pythonan association like this class Parent List children static hasMany =[children:Child]

I have 开发者_如何学Pythonan 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号