Note that there are some explanatory texts on larger screens.

plurals
  1. POJPA OneToMany eager fetch does not work
    text
    copied!<p>I have a weird problem with two entities with one-to-many relation in JPA. I am using Glassfish 3.1.2.2 with EclipseLink 2.3.2. This is the first entity:</p> <pre><code>@NamedQueries({ @NamedQuery(name="SampleQueryGroup.findAll", query="SELECT g FROM SampleQueryGroup g") }) @Entity public class SampleQueryGroup implements Serializable { // Simple properties, including id (primary key) @OneToMany( mappedBy = "group", fetch = FetchType.EAGER, cascade = {CascadeType.REMOVE, CascadeType.MERGE} ) private List&lt;SampleQuery&gt; sampleQueries; // Gettes/setters, hashcode/equals } </code></pre> <p>And this is the second one:</p> <pre><code>@Entity public class SampleQuery implements Serializable { // Simple properties, including id (primary key) @ManyToOne(cascade = {CascadeType.PERSIST}) private SampleQueryGroup group; // Gettes/setters, hashcode/equals } </code></pre> <p>I have a stateless session bean which uses an injected <code>EntityManager</code> to run <code>SampleQueryGroup.findAll</code> named query. I also have a CDI managed bean which calls the SSB method and iterates through <code>SampleQueryGroup.getSampleQueries()</code> for each <code>SampleQueryGroup</code> returned by the method. I didn't paste the code as it is pretty straightforward and somehow standard for any Java EE application.</p> <p>The problem is the eager fetch does not work and <code>getSampleQueries()</code> returns an empty list. However, when I change the fetch type back to FetchType.LAZY, everything works and I get the list correctly populated. I don't understand why this happens. Does it have anything to do with internal caching mechanisms?</p>
 

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