JPA 2.1 Entity Graph returns duplicated results
entitymanager, hibernate, jakarta-ee, jpa, wildfly
Solution
I finally solved this, I added DISTINCT to the named query and everything works now.... The error was caused because when the JPA Provider find a entity graph hint, it creates a LEFT JOIN with the child table. Original Query without entity graph:
11:55:28,950 INFO [stdout] (default task-23) Hibernate:
11:55:28,950 INFO [stdout] (default task-23) select
11:55:28,951 INFO [stdout] (default task-23) entitya0_.id as id1_0_0_,
11:55:28,951 INFO [stdout] (default task-23) childs1_.id as id1_1_1_,
11:55:28,951 INFO [stdout] (default task-23) entitya0_.filter as filter2_0_0_,
11:55:28,951 INFO [stdout] (default task-23) childs1_.parent_id as parent_i2_1_1_,
11:55:28,951 INFO [stdout] (default task-23) childs1_.parent_id as parent_i2_0_0__,
11:55:28,951 INFO [stdout] (default task-23) childs1_.id as id1_1_0__
11:55:28,951 INFO [stdout] (default task-23) from
11:55:28,951 INFO [stdout] (default task-23) EntityA entitya0_
11:55:28,951 INFO [stdout] (default task-23) left outer join
11:55:28,952 INFO [stdout] (default task-23) EntityB childs1_
11:55:28,952 INFO [stdout] (default task-23) on entitya0_.id=childs1_.parent_id
11:55:28,952 INFO [stdout] (default task-23) where
11:55:28,952 INFO [stdout] (default task-23) entitya0_.filter like ?
Query with distinct and entity graph:
11:57:25,051 INFO [stdout] (default task-24) Hibernate:
11:57:25,052 INFO [stdout] (default task-24) select
11:57:25,052 INFO [stdout] (default task-24) distinct entitya0_.id as id1_0_0_,
11:57:25,052 INFO [stdout] (default task-24) childs1_.id as id1_1_1_,
11:57:25,052 INFO [stdout] (default task-24) entitya0_.filter as filter2_0_0_,
11:57:25,052 INFO [stdout] (default task-24) childs1_.parent_id as parent_i2_1_1_,
11:57:25,052 INFO [stdout] (default task-24) childs1_.parent_id as parent_i2_0_0__,
11:57:25,052 INFO [stdout] (default task-24) childs1_.id as id1_1_0__
11:57:25,052 INFO [stdout] (default task-24) from
11:57:25,052 INFO [stdout] (default task-24) EntityA entitya0_
11:57:25,052 INFO [stdout] (default task-24) left outer join
11:57:25,052 INFO [stdout] (default task-24) EntityB childs1_
11:57:25,052 INFO [stdout] (default task-24) on entitya0_.id=childs1_.parent_id
11:57:25,052 INFO [stdout] (default task-24) where
11:57:25,052 INFO [stdout] (default task-24) entitya0_.filter like ?
Problem
I started using the new entity graph feature in JPA 2.1 to specify the Lazy collections that must be loaded. Consider following classes: ``` @Entity @NamedQueries({ @NamedQuery(name="findWithFilterAttr","select a from A a where a.filterAttribute like :filter") }) @NamedEntityGraphs({ @NamedEntityGraph(name = "graph.childs", attributeNodes = @NamedAttributeNode("childs"))}) public class A{ @Id private long id; @OneToMany(mappedBy="parent") private List<B> childs; private String filterAttribute; } @Entity public class B{ @Id private long id; @ManyToOne private A parent; } ``` When I execute the named query to obtain a list of A entities with the entity graph hint I get a collection with repeated A entities. How can I load the A entities only once. I'm using: - Hibernate 4.3.5 - Wildfly 8.1