2

我有一个类似的问题。但我想要一个不包含功能的。

就像我有一个 Post 域。一个帖子有很多用户。我想做的,使用 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 的列表,其 users 字段是一个集合不包含thisUser

4

3 回答 3

2

你可以用HQL这个

List<Post> posts = Post.executeQuery("select distinct p from Post p where :myUser not member of p.users", [myUser: user, max: maxVar])
于 2014-06-13T17:33:26.320 回答
0
c.list (max: maxVar) { not { 'in'("users", thisUser) }}

将返回一个没有 thisUser 的列表。这适用于 grails 版本 2.*。您可能需要为您的 User 类覆盖 .equals 。

还有许多其他方法,例如使用 .find。请参阅此链接http://groovy.codehaus.org/JN1015-Collections

于 2014-06-13T04:13:53.310 回答
0

我无法获得传统的解决方案,但我使用两个单独的查询解决了这个问题。

首先,我得到了用户包含thisUser所有帖子

users { eq('id', thisUser.id) }

然后我得到了最大数量的帖子,其 id 与上面的任何 id 都不匹配。

Post.findAllByIdNotInList(usersList.id, [max:15])

其中usersList是第一次查询的结果

PS:如果有人有更好的解决方案,请回答。谢谢

于 2014-06-13T12:39:10.713 回答