Gorm嵌套查询包含Any

时间:2014-04-11 14:39:57

标签: grails gorm

我有一个域类

Post {
hasMany [comments : Comment]
}

Comment {
 belongsTo [post: Post]
 User user
}

我想搜索包含特定用户评论的所有帖子。

这样的东西
        def posts = Post.findAll
        {(
            //condition1 ||
            // condition 2 ||
            comments.containsAny(Comment.findByUser(User.get(params.userId)))
//if the post contains any comment from this user, get it
        )}

知道我怎么能这样做吗?

谢谢

1 个答案:

答案 0 :(得分:1)

此条件查询应该这样做

def user = User.get(params.userId)

def posts = Post.createCriteria().listDistinct {

  comments {
    eq 'user', user
  }
}

listDistinct确保如果用户多次对帖子发表评论,则只会检索一次帖子。

相关问题