Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple fetches with EAGER type in Hibernate with JPA
    primarykey
    data
    text
    <p>I have an entity which contains:</p> <pre><code>@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "assessment") @OrderBy(value = "order ASC") private List&lt;AssessmentPart&gt; assessmentParts = new LinkedList&lt;&gt;(); @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "assessment") private List&lt;AssessmentText&gt; texts = new LinkedList&lt;&gt;(); </code></pre> <p>as you see there are two collections which needs to be eagerly loaded. This is not working and hibernate throws an exception:</p> <pre><code>Caused by: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags </code></pre> <p>That's because Hibernate can't fetch multiple collections with one go. But if I change the <code>List</code> to <code>Set</code> and the <code>LinkedList</code> to <code>HashSet</code> this part works fine but the other - more annoying issue occurs.</p> <p>When I am trying to get the entity from the database using:</p> <pre><code>entityManager.find(entityClass, primaryKey); </code></pre> <p>It fails with:</p> <pre><code>org.hibernate.AssertionFailure: null identifier </code></pre> <p>I am sure that ID I am passing to <code>find</code> method is not null, I've debugged and I am sure of this. It somehow disappears inside Hibernate.</p> <p>If I change collection types to <code>LAZY</code> everything just works without errors but there are some circumstances where I need to use <code>EAGER</code>.</p> <p>Does anybody have a solution how to fix it? Either I could have a set but prevent assertion error from occurring or I could have a list but somehow avoid multiple fetch bags error.</p> <p>I am using:</p> <pre><code>Hibernate 4.2.2.Final Tomcat 7 JPA 2.0 JDK 1.7 </code></pre> <p><strong>EDIT</strong></p> <p>I've just discovered that adding <code>@Fetch(FetchMode.SELECT)</code> fixes the issue and I can use multiple list with <code>EAGER</code> type, but is there anyway to solve this by not using Hibernate specific annotations? And why it fixed the issue in the first place?</p>
    singulars
    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.
    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