我有以下查询,我正在使用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>();
}
在上面的查询中,我正在尝试获取ProjectPage
及其categorySet
,如上所示,类Category
包含一组类型,因此每{{1}对象将包含一组ProjectPage
,并且此集合中的每个对象都将包含一组Category
,现在问题是当我检索Category
对象时的条件在哪里子句仅应用于ProjectPage
的第一级集合而不是每个Category
内的每个集合,我想进行递归查询,以便我可以将where条件应用于第n级而不是使用代码,我试图使用拦截器,但不起作用,任何想法如何做到这一点?
答案 0 :(得分:0)
当您在WHERE子句中引用LEFT JOIN列时,WHERE条件将始终过滤掉空值。所以最终结果是INNER JOIN。
JPA和Hibernate都不支持递归查询,因为there's no one and only one standard implementation amongst all databases Hibernate supports。
如果您使用PostgreSQL,可以使用Common Table Expression。