2014-04-11 49 views
1

我有一個域類格姆嵌套查詢中包含的任何

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

此條件查詢應該這樣做

def user = User.get(params.userId) 

def posts = Post.createCriteria().listDistinct { 

    comments { 
    eq 'user', user 
    } 
} 

listDistinct確保如果在後幾次用戶評論,帖子只檢索一次。