Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 348
DaveyDaveDave Avatar asked Jun 29 '10 10:06

DaveyDaveDave


People also ask

Can a JPA entity have multiple OneToMany associations?

You can have multiple one-to-many associations, as long as only one is EAGER.

What is difference between JPA unidirectional OneToOne and ManyToOne?

The main difference between a OneToOne and a ManyToOne relationship in JPA is that a ManyToOne always contains a foreign key from the source object's table to the target object's table, whereas a OneToOne relationship the foreign key may either be in the source object's table or the target object's table.

What is difference between MappedBy and @JoinColumn?

The @JoinColumn annotation helps us specify the column we'll use for joining an entity association or element collection. On the other hand, the mappedBy attribute is used to define the referencing side (non-owning side) of the relationship.

How do you persist many-to-many relationships in JPA?

In JPA we use the @ManyToMany annotation to model many-to-many relationships. This type of relationship can be unidirectional or bidirectional: In a unidirectional relationship only one entity in the relationship points the other. In a bidirectional relationship both entities point to each other.


1 Answers

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.

like image 119
DaveyDaveDave Avatar answered Oct 14 '22 10:10

DaveyDaveDave