Jils
Jils

Reputation: 783

Grails/CreateCriteria - compare two lists

By using the CreateCriteria, I want to compare two lists and check if there are at least one element in groups present in users. Is there something like eq to perform that?

Domain

class User {
    String login
    static hasMany = [groups = String]
} 

class Project {
    String  name
    static hasMany = [users = User]
}

CreateCriteria

def UserInstance = User.get(1)

def idList =  Project.createCriteria().list () {

    projections { distinct ( "id" )
        property("name")
        property("id")
    }

    eq("users.login", UserInstance.groups) //check if there are at least one element in groups list present in users list.  
    order("name","desc")

}

Upvotes: 0

Views: 663

Answers (1)

Emmanuel Rosa
Emmanuel Rosa

Reputation: 9885

Yes, you can use inList(String propertyName, Collection c) like this:

def UserInstance = User.get(1)

def idList =  Project.withCriteria {

    projections { 
        distinct("id")
        property("name")
        property("id")
    }

    users {
        inList("login", UserInstance.groups)
    }

    order("name","desc")
}

Upvotes: 2

Related Questions