T3rm1 (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
) *created* an issue
Hibernate ORM (
https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiNDUwMmIzNDUz...
) / Bug (
https://hibernate.atlassian.net/browse/HHH-16186?atlOrigin=eyJpIjoiNDUwMm...
) HHH-16186 (
https://hibernate.atlassian.net/browse/HHH-16186?atlOrigin=eyJpIjoiNDUwMm...
) Duplicate entries in OneToMany relation after manual eagerly loading of entries (
https://hibernate.atlassian.net/browse/HHH-16186?atlOrigin=eyJpIjoiNDUwMm...
)
Issue Type: Bug Assignee: Unassigned Attachments: bug.zip Created: 15/Feb/2023 06:24 AM
Priority: Major Reporter: T3rm1 (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
)
I’m not sure if the title fits. I had trouble finding a good one.
This bug leads to multiple duplicates appear in the list of a OneToMany relation that was
initialized manually in order to prevent N+1 query problem.
There are three entities, A , B and C.
A < 1 1 > B
B < 1 N > C
A has one B, B has multiple C. Loading is configured as lazy.
*Given situation* : I have 4 {{A}}which all point to the same B. This {{B}}has two C. So
if I do {{a.getB().getCList()}}the size should be 2. This works out of the box with lazy
loading.
However, if I add the following query, in order to load all {{C}}into the cache and a
prevent N+1 query problem, then suddenly each {{B}}has *eight* entries of C.
List<A> resultNotNeeded = entityManager.createQuery("""
select distinct a from A a
join fetch a.b b
left join fetch b.cList
where a in (:aList)
""", A.class)
.setParameter("aList", aList)
.setHint(org.hibernate.jpa.QueryHints.HINT_PASS_DISTINCT_THROUGH,
"false")
.getResultList();
It doesn’t make a difference if I remove the distinct and query hint. {{aList}}is just the
list of the four a entities.
A test case is attached.
(
https://hibernate.atlassian.net/browse/HHH-16186#add-comment?atlOrigin=ey...
) Add Comment (
https://hibernate.atlassian.net/browse/HHH-16186#add-comment?atlOrigin=ey...
)
Get Jira notifications on your phone! Download the Jira Cloud app for Android (
https://play.google.com/store/apps/details?id=com.atlassian.android.jira....
) or iOS (
https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=Em...
) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100216- sha1:a1e7611 )