Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA 2.1 Entity Graph returns duplicated results

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
like image 676
cristhiank Avatar asked Nov 27 '14 11:11

cristhiank


2 Answers

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 ?
like image 73
cristhiank Avatar answered Jan 04 '23 12:01

cristhiank


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:

  • adding limits to the query
  • using distinct
like image 44
Jidehem Avatar answered Jan 04 '23 13:01

Jidehem