我有一个域名类(class)
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
)}
知道我该怎么做吗?
谢谢
最佳答案
此条件查询应执行此操作
def user = User.get(params.userId)
def posts = Post.createCriteria().listDistinct {
comments {
eq 'user', user
}
}
listDistinct
可确保如果用户对某个帖子发表几次评论,则该帖子只会被检索一次。
关于grails - Gorm嵌套查询包含任何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23015688/