简体   繁体   中英

Hibernate JPA recursive query

I have the following query, I'm using hibernate a JPA provider:

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();

and below are the entities of ProjectPage and Category respectively:

@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>();

}

in the above query, i'm trying to fetch a ProjectPage along with its categorySet and as shown above class Category contains a set of its type, so every ProjectPage object will contain a set of Category and each object inside this set will contains a set of Category , now the problem is that when i retrieve the ProjectPage object the conditions in the where clause applied only on the first level set of Category not on each set inside each Category , i want to make a recursive query so that i can apply the where condition to the nth level instead of doing that with code, i tried to use interceptor but doesn't work, any idea of how to do that?

  1. The WHERE condition will always filter out nulls when you reference a LEFT JOIN column in your WHERE clause. So the end result is an INNER JOIN.

  2. Neither JPA nor Hibernate support recursive queries, because there's no one and only one standard implementation amongst all databases Hibernate supports .

  3. In case you use PostgreSQL, you can use a Common Table Expression .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM