2015-10-07 31 views
0

通過使用CreateCriteria,我想比較兩個列表並檢查users中是否存在至少一個groups中的元素。 有沒有像eq這樣的表演?Grails/CreateCriteria - 比較兩個列表


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

} 

回答

2

是的,你可以使用inList(String propertyName, Collection c)這樣的:

def UserInstance = User.get(1) 

def idList = Project.withCriteria { 

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

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

    order("name","desc") 
}