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:
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 ?
A small complement.
I had the same problem as you.
I think this behaviour is unexpected and may be a flaw in hibernate.
It shouldn't be needed to specify distinct
as it's the case for fetches directly specified in the JPQL query.
Your solution is rather a workaround because of the implementation's limitation.
By checking the code (computation of needsDistincting
flag in method list()
, available at grepcode), there are 2 solution:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With