JPA 2.1 Entity Graph returns duplicated results

怎甘沉沦 提交于 2019-11-30 20:32:52

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:

  • adding limits to the query
  • using distinct
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!