2015-10-13 40 views
2

我使用休眠,並希望獲得用戶 - >帖子 - >評論的整個層次結構,但只有具有「狀態」爲真的評論。休眠:獲取整個層次結構與過濾子集合

我已經嘗試過這樣的事情:

Select user from User user join user.posts post join post.comments comment where comment.status = true 

但是,這給我的用戶的爆炸爲與真實身份的評論的數量和單用戶>門柱內側,我所有的評論,不僅是狀態爲真的那些。

我也嘗試過「左連接」或「內連接」,但我得到了相同的結果。

有沒有辦法得到我想要的?這是乾淨的嗎?

這裏是類結構的一個小例子:

Class User 
List<Post> posts 

Class Post 
List<Comment> comments 

Class Comment 
String text 
boolean status 

感謝您的幫助!

回答

1

我想你想要做的使用@FilterJoinTable註釋過濾收集什麼:

@FilterJoinTable(name="activeComments", condition="status = :status") 
List<Comment> comments 

您需要聲明類級別的過濾器:

@FilterDef(name="activeComments", parameters={ 
    @ParamDef(name="status", type="boolean") 
}) 

和啓用會話:

Filter filter = session.enableFilter("activeComments"); 
    filter.setParameter("status", true); 

這裏是在休眠濾波的教程,與實施例: http://www.concretepage.com/hibernate/hibernate-filter-and-filterjointable-annotation-example

...和官方休眠3文檔: https://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/filters.html