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}}.
{ noformat code:java }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();{ noformat code }
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. |
|