2015-01-15 871 views
0

我有以下查詢,我使用Hibernate JPA提供:Hibernate的JPA遞歸查詢

entityManager().createQuery(
      "SELECT page FROM ProjectPage page" 
      +" left join fetch page.categorySet as category" 
      + " where page.id = :id " 
      + " and category.parentCategory is null " 
      + " and (category.status is null or category.status != :status_val) " 
      ,ProjectPage.class).setParameter("id", id).setParameter("status_val", Status.DELETED).getSingleResult(); 

及以下的ProjectPage分別實體和Category

@Entity 
@Table(name="project_page") 
@Configurable 
public class ProjectPage { 

@OneToMany(mappedBy = "parentPage") 
private Set<Category> categorySet = new HashSet<Category>(); 
} 


@Configurable 
@Table(name="category") 
@Entity 
public class Category{ 

@OneToMany(cascade = CascadeType.ALL, mappedBy = "parentCategory",fetch=FetchType.EAGER) 
private Set<com.se.dataadminbl.model.Category> categorySet = new HashSet<com.se.dataadminbl.model.Category>(); 

} 
在上面

查詢時,我試圖取得與沿ProjectPagecategorySet並如上圖所示Category類包含一組它的類型,所以每ProjectPage對象將包含一套Category一第二本集將裏面的每個對象包含了一組Category,現在的問題是,當我取回ProjectPage對象,其中僅適用於不能在每一個每個Category內設置的第一級組的Category子句中的條件,我想做一個遞歸查詢,以便我可以將where條件應用到第n個級別,而不是用代碼來做到這一點,我試圖使用攔截器,但沒有工作,任何想法如何做到這一點?

+0

你想到的是強制執行'WHERE'在一個'OneToMany'關係的收集條件將允許您訪問過濾行(根據條件)從集合 - 在你的情況,'categorySet'( '設置')通過父實體'頁面?不,它不會發生。所以,請嘗試改變方法。請不要也使用與'的'OneToMany'關係JOIN FETCH'(雖然它是由一些JPA提供商都支持)來過濾行(通過目標實體(有外鍵的一個)導航)別名使用,否則會改變對象的構建方式 – Tiny 2015-01-15 19:28:55

回答