Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate JPA - ManyToOne relationship not populated
    primarykey
    data
    text
    <p>I'm currently moving a (working) app from using EclipseLink to Hibernate JPA, mostly it's gone quite smoothly, but I'm finding one thing that I can't explain, and also can't think of any good search terms!</p> <p>Basically, I have four entities, with one-to-many relationships forming a chain:</p> <p>EntityA has a list of EntityB's, each of which has a list of EntityC's, each of which have a list of EntityD's</p> <p>each of those then has a many-to-one relationship going the other way, so:</p> <p>EntityD has an EntityC, which has an EntityB, which has an EntityA.</p> <p>That is (heavily reduced for clarity):</p> <pre><code>@Entity public class EntityA { @OneToMany (cascade = CascadeType.All, mappedBy = "entityA") private List&lt;EntityB&gt; entityBList; ... } @Entity public class EntityB { @OneToMany (cascade = CascadeType.All, mappedBy = "entityB") private List&lt;EntityC&gt; entityCList; @JoinColumn (name = "ENTITY_A", referencedColumnName = "ENTITY_A_ID") @ManyToOne (cascade = CascadeType.PERSIST, optional = false) private EntityA entityA; } @Entity public class EntityC { @OneToMany (cascade = CascadeType.ALL, mappedBy = "entityC") private List&lt;EntityD&gt; entityDList; @JoinColumn (name = "ENTITY_B", referencedColumnName = "ENTITY_B_ID") @ManyToOne (cascade = CascadeType.PERSIST, optional = false) private EntityB entityB; } @Entity public class EntityD { @JoinColumn (name = "ENTITY_C", referencedColumnName = "ENTITY_C_ID") @ManyToOne (cascade = CascadeType.PERSIST, optional = false) private EntityC entityC; } </code></pre> <p>I get an EntityA from the database (looking it up by its primary key), and thus get a nicely populated EntityA instance, with a PersistentBag for my <code>List&lt;EntityB&gt;</code>. I see a lazy load happening when I dereference that <code>List&lt;EntityB&gt;</code>, and the same repeated for getting EntityCs from EntityB.</p> <p>At this point, everything is as I expect, I have an EntityA, B and C all fully populated with the values from the database, but then I try to get my EntityD, from EntityC, and find that it's null.</p> <p>My entity manager is still open and active at this point, and even if I look at it in the debugger immediately after getting the EntityA, I can walk through the relationships, as far as EntityC, and again see the 'entityDList' as null.</p> <p>The only solution I've found so far is to use:</p> <pre><code>EntityManager.refresh(entityC); </code></pre> <p>which populates all its elements including a lazily-loaded PersistentBag for the entityDList.</p> <p>So, my guess is that Hibernate is only populating the references 2 levels deep (or 3, depending on how you count), and giving up after that, although I don't really understand why that would be. Does that make sense to anyone?</p> <p>Is there any solution other than the .refresh? Some kind of config or annotation value that will make Hibernate populate the references all the way down?</p>
    singulars
    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