在createCriteria的域对象的集合属性中是否存在“不包含”功能? [英] Is there a 'does not contains' functionality on a collection property of a domain object for createCriteria?
本文介绍了在createCriteria的域对象的集合属性中是否存在“不包含”功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到类似于此。但是我想要一个不包含功能。
像我有一个Post域。帖子 hasMany 用户。
使用createCriteria,我想要做的是这样的:
def c = Post.createCriteria ()
def l = c.list(max:maxVar){
notContains(users,thisUser)
}
我试过使用 ne 但没有运气。
def l = c.list(max:maxVar){
users {
ne('id',thisUser.id)
}
}
要清楚的是,我如何获得其所有用户字段是集合的所有Post的列表不包含 thisUser ?
解决方案
您可以使用 HQL
列表< Post> posts = Post.executeQuery(从Post p选择不同的p,其中:myUser不是p.users的成员,[myUser:user,max:maxVar])
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 ?
解决方案
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])
这篇关于在createCriteria的域对象的集合属性中是否存在“不包含”功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文