grailscreatecriteria

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")

}

Solution

  • 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")
    }