Note that there are some explanatory texts on larger screens.

plurals
  1. POjpa criteria query duplicate values in fetched list
    primarykey
    data
    text
    <p>I'm observing what I think is an unexpected behaviour in JPA 2 when fetching a list attribute with a criteria query.</p> <p>My query is as follows (an extract of it):</p> <pre class="lang-java prettyprint-override"><code>CriteriaBuilder b = em.getCriteriaBuilder(); CriteriaQuery&lt;MainObject&gt; c = b.createQuery(MainObject.class); Root&lt;MainObject&gt; root = c.from(MainObject.class); Join&lt;MainObject, FirstFetch&gt; firstFetch = (Join&lt;MainObject, FirstFetch&gt;) root.fetch(MainObject_.firstFetch); firstFetch.fetch(FirstFetch_.secondFetch); //secondFetch is a list c.select(root).distinct(true); </code></pre> <p>(So let's say I'm fetching a list as a property of the property of an object.)</p> <p>The thing is when the query returns multiple results, secondFetch values are duplicated as many times as rows are returned. Each <em>firstFetch</em> should have just one <em>secondFetch</em> but has <strong>n</strong> instead. The only particularity i see in this case is all <em>MainObjects</em> happen to have the same FirstFetch instance. So my guess is the join is being crossed, which is normal, but then JPA fails to assign its <em>secondFetch</em> object to each one of the <em>firstFetchs</em>.</p> <p>Mappings shouldn't be too special, the're more or less like this</p> <pre class="lang-java prettyprint-override"><code>@Entity @Table(name="mainobject") public class MainObject{ //... private FirstFetch firstFetch; @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name="mainObject_column") public FirstFetch getFirstFetch() { return firstFetch; } } </code></pre> <p>and</p> <pre class="lang-java prettyprint-override"><code>@Entity @Table(name="firstFetch") public class FirstFetch{ //... private List&lt;SecondFetch&gt; secondFetch; @OneToMany(mappedBy="secondFetch") public List&lt;SecondFetch&gt; getSecondFetch() { return secondFetch; } } </code></pre> <p>&amp; finally</p> <pre class="lang-java prettyprint-override"><code>@Entity @Table(name="secondFetch") public class SecondFetch { //.... private FirstFetch firstFetch; //bidirectional @ManyToOne @JoinColumn(name="column") public FirstFetch getFirstFetch() { return firstFetch; } } </code></pre> <p>I've been looking for some sort of <strong>distinct</strong> sentence to apply to the fetch but there's none (would have been a 'patch' anyway...)</p> <p>If i change</p> <pre class="lang-java prettyprint-override"><code>List&lt;SecondFetch&gt; </code></pre> <p>for</p> <pre class="lang-java prettyprint-override"><code>Set&lt;SecondFetch&gt; </code></pre> <p>i'll get the expected result thanks to Sets' <strong>Keys</strong>, so I do feel this is kind of a misbehaviour in JPA's lists.</p> <p>I'm not an expert, though, so i could perfectlly be making some mistake in the mappings or query. Any feeback is very welcome to help clear this out. Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload