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!
Basically, I have four entities, with one-to-many relationships forming a chain:
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
each of those then has a many-to-one relationship going the other way, so:
EntityD has an EntityC, which has an EntityB, which has an EntityA.
That is (heavily reduced for clarity):
@Entity
public class EntityA {
@OneToMany (cascade = CascadeType.All, mappedBy = "entityA")
private List<EntityB> entityBList;
...
}
@Entity
public class EntityB {
@OneToMany (cascade = CascadeType.All, mappedBy = "entityB")
private List<EntityC> 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<EntityD> 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;
}
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 List<EntityB>
. I see a lazy load happening when I dereference that List<EntityB>
, and the same repeated for getting EntityCs from EntityB.
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.
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.
The only solution I've found so far is to use:
EntityManager.refresh(entityC);
which populates all its elements including a lazily-loaded PersistentBag for the entityDList.
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?
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?