是否有助手知道属性是否已被Hibernate加载? [英] Is there a helper to know whether a property has been loaded by Hibernate?
问题描述
我需要一个助手来知道一个属性是否已经被加载,以避免 LazyInitializationException
。
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());
推荐答案
实际上有两种方法。
为了找出懒惰的属性是否已经初始化,您可以调用 Hibernate.isPropertyInitialized()$ c $
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.
若要确定是否有一个懒惰的集合(或实体)已经初始化(就像你的例子),你可以调用 Hibernate.isInitialized()
,集合(实体)实例作为参数。
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.
这篇关于是否有助手知道属性是否已被Hibernate加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!