Is there a 'contains' functionality on a collection property of a domain object for createCriteria?
Asked Answered
M

1

11

I have an Auction domain object and a User domain object. An Auction hasMany Users.

What I'd like to do, using createCriteria, is something like this:

def c = Auction.createCriteria()
def l = c.list (max: maxVar, offset: offsetVar) {
    contains("users", thisUser)
}

Though, contains is not in the list of acceptable nodes: createCriteria description page.

Is there any way to implement this functionality?

To be clear, is there a way to have the criteria be that a specified User object is contained within a collection property of the Auction?

Melloney answered 13/7, 2012 at 17:0 Comment(0)
A
12

Try this:

def l = c.list (max: maxVar, offset: offsetVar) {
    users {
        idEq(thisUser.id)
    }
}
Aylmar answered 13/7, 2012 at 17:4 Comment(1)
Nice... it's not obvious that you can do this, since the syntax looks like "must have a users object whose id is this", as opposed to "must have a users collection which contains an object with this id".Clayson

© 2022 - 2024 — McMap. All rights reserved.