'Hibernate / JPA EntityGraph fetchGraph does not fetch all entities because it is based on an innerjoin
I have a problem with EntityGraph on CriteriaApi. Here is what I am doing:
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
EntityGraph<LabelFamily> entityGraph = entityManager.createEntityGraph(LabelFamily.class);
entityGraph.addAttributeNodes("childs");
CriteriaQuery<LabelFamily> criteria = builder.createQuery(LabelFamily.class);
Root<LabelFamily> fromPost = criteria.from(LabelFamily.class);
criteria.select(fromPost);
criteria.where(specification.toPredicate(fromPost, criteria, builder));
return entityManager.createQuery(criteria)
.setHint("javax.persistence.fetchgraph", entityGraph)
.getResultList();
I have some specifications in my criteria that defines an inner join on the "childs" (It's legacy code). Then i want to load every child entity (defined by an OneToMany) in one request, therefore i use EntityGraph. My problem is this: EntityGraph use the inner join declared in my specifications but it is used to filter the result of the request.
Here is the sql request to better understand the underlying problem:
select distinct labelfamil0_.*,
childs1_.*
from label_family labelfamil0_
inner join label_family childs1_ on labelfamil0_.id = childs1_.family_parent_id
inner join flow flows2_ on childs1_.id = flows2_.label_family_project_id
cross join project project3_
where X
But as we can see the relation childs1_ is filtered through an other inner join.
The result I want is something like this:
select distinct labelfamil0_.*,
childs2_.*
from label_family labelfamil0_
inner join label_family childs2_ on labelfamil0_.id = childs2_.family_parent_id
inner join label_family childs1_ on labelfamil0_.id = childs1_.family_parent_id
inner join flow flows2_ on childs1_.id = flows2_.label_family_project_id
cross join project project3_
where X
I want to filter my "parent" entity using a filter on its child, but then i want all the childs of my resulting parents.
I have the correct result if I do not use an entityGraph and I simply EAGER (or LAZY) fetch the relation. So if I simply do that:
parent.getChilds()
Without using EntityGraph, I get all childs as I wanted
I am using MySQL 5.7, i do not know if that is of any importance. Version of Spring: 2.5.8 (I use Hibernate imported from the parent pom)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
