I need a helper to know whether a property has been loaded as a way to avoid LazyInitializationException. Is it possible?
@Entity
public class Parent {
@OneToMany
private List<Child> childList;
}
@Entity
public class Child {
}
"select distinct p from Parent p left join fetch p.childList";
// Answer goes here
// I want to avoid LazyInitializationException
SomeHelper.isLoaded(p.getChildList());
There are two methods, actually.
To find out whether a lazy property has been initialized you can invoke
Hibernate.isPropertyInitialized()method with your entity instance and property name as parameters.To find out whether a lazy collection (or entity) has been initialized (like in your example) you can invoke
Hibernate.isInitialized()with collection (entity) instance as parameter.