DaveyDaveDave
DaveyDaveDave

Reputation: 10612

Hibernate JPA - ManyToOne relationship not populated

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?

Upvotes: 7

Views: 7299

Answers (3)

DaveyDaveDave
DaveyDaveDave

Reputation: 10612

Thanks to the suggestions from people here, which are probably relevant, but didn't help my specific case.

If you're reading this experiencing the same problem, it's probably worth trying the max_fetch_depth suggestion, but for some reason it didn't work for me (I'd love suggestions as to why?).

Likewise, if your @OneToManys are Sets, rather than Lists, doing an eager fetch or a left join, as suggested by Albert might work, but apparently Hibernate only lets you have a maximum of 1 List that is eagerly fetched, if you need more than that, your collections should be Sets. I didn't try it, but I suspect that it might have solved the problem.

Unless anyone has a better suggestion, I'll stick with calling refresh, which actually probably makes more sense for my application anyway.

Upvotes: 3

ambassador86
ambassador86

Reputation: 161

The hibernate docs say that you can set it with the hibernate.max_fetch_depth property. The default is 3. You can find it in the "Hibernate Reference Documentation" on page 47.

Upvotes: 1

Albert
Albert

Reputation: 2115

This is funny indeed. One way to work around it would be to query object A left join-fetching to ->B->C->D which is also faster if your going to traverse down to object D anyway. It would be something like this.

"from A left join fetch B left join fetch C left join fetch D"

Have you also tried making the relationship from C->D eager? Curious what will happen then...

Upvotes: 1

Related Questions