I have three Entities A, B and C.
@Entity
public class A {
@Id
private long id;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "ID", insertable = false, updatable = false)
public B b;
}
@Entity
public class B {
@Id
private long id;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID", insertable = false, updatable = false)
public C c;
}
@Entity
public class C {
@Id
private long id;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ID", insertable = false, updatable = false)
public B b;
}
So there is an eagerly loaded reference of B in A and between B and C is a lazily loaded bidirectional relation.
My problem: When fetching an instance of A from the DB, an instance of C is fetched as well by Hibernate.
I discovered it in a heap dump due to a memory leak and tested it programmatically:
@Test
public void eagerAndLazyLoading() {
A a = dao.getA(1L);
Assert.assertNotNull(a);
PersistenceUnitUtil puu = emf.getPersistenceUnitUtil();
Assert.assertTrue(puu.isLoaded(a));
Assert.assertTrue(puu.isLoaded(a, "b"));
Assert.assertFalse(puu.isLoaded(a.b, "c"));
}
That last Assert fails.
Luckily there was no problem getting rid of the bidirectional relation, but I would like to know the source of the problem. Is it the intended behaviour of Hibernate to eagerly load all references of an entity, that is eagerly loaded?