我有以下查询,我正在使用 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
类包含一组其类型,因此每个ProjectPage
对象将包含一组Category
,并且该集合内的每个对象将包含一组Category
,现在的问题是,当我检索ProjectPage
时code> 对象 where 子句中的条件仅应用于 Category
的第一级集合,而不是应用于每个 Category
内的每个集合,我想进行递归查询,以便我可以将 where 条件应用于第 n 级,而不是使用代码执行此操作,我尝试使用拦截器,但不起作用,知道如何做到这一点吗?
最佳答案
当您在 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 .
关于java - Hibernate JPA 递归查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27970474/