Most of our Entities have Lazy loading as they are not required. In one specific scenario,function requires them to load eagerly, which is out of Transaction Scope.
Here's the bit of implementation
@Transaction
public E getFullyLoadedDetachedEntity(Long id) {
E entity = dao.findById(id);
Hibernate.initialize(entity);
dao.detach(entity);
return entity;
}
But the problem is, Entity(E) might have associated entities 'X' which might have 'Y' as lazy loaded.
class E{
X x; //Lazy Load
}
class X{
Y y; //Lazy load
}
Now, Hibernate.initialize(entity) may be loading only it's associated entities(X) but not the associated entites(Y) of X.
How do I forcefully load all of the dependencies/Lazy loaded objects at any level.
Update I tried this one though
E entity = dao.findById(id);
Hibernate.initialize(entity);
for (X x : entity.getXList()) {
Hibernate.initialize(x);
}
dao.detach(entity);
I am still getting LazyException on accessing the X.getY()