grailshqlcreatecriteria

Is there a 'does not contains' functionality on a collection property of a domain object for createCriteria?


I have a problem similar to this. But I want a does not contain functionality.

Like I have a Post domain. A Post hasMany User. What I'd like to do, using createCriteria, is something like this:

def c = Post.createCriteria()
def l = c.list (max: maxVar) {
    notContains("users", thisUser)
}

I tried using ne But no luck.

def l = c.list (max: maxVar) {
    users {
        ne('id', thisUser.id)
    }
}

To be clear, how can I get list of all the Post whose users field which is a collection does not contain thisUser ?


Solution

  • You can use HQL for this

    List<Post> posts = Post.executeQuery("select distinct p from Post p where :myUser not member of p.users", [myUser: user, max: maxVar])