I've got this mappings in Hibernate (through JPA, using EntityManager and such):
public ChildClass {
....
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parentClassId")
private ParentClass parentClass;
....
}
And a parent class that has a list of ChildClass
elements:
public ParentClass {
...
@OneToMany(mappedBy = "parentClass", fetch = FetchType.LAZY)
private List<ChildClass> childElements;
...
}
Now I have this method in my Service layer:
@Transactional
public ChildClassDTO consultarPuntuacionPrueba(Long parentClassId) {
ChildClass result= childClassDAO.getChildClassByParentId(parentClassId);
ParentClass parentClass = result.getParentClass();
System.out.println("UNITIALIZED: " + ((HibernateProxy)prueba).getHibernateLazyInitializer().isUninitialized());
Long parentClassId = parentClass.getParentClassId();
Boolean anAttribute = parentClass.getBooleanAttribute();
return map(result, ChildClassDTO.class);
}
The problem is -- parentClass is a javassist proxy, and even though when accessing a getter of parentClass
, I can see a query getting executed (it shows in my console), the proxy never gets initialized and holds null for all attributes... I have tried said query directly towards my database, and it returns the expected data.
I know Hibernate must return a proxy when I call childClass.getParentClass()
, but why is it never initialized afterwards?