有没有一个助手可以知道 Hibernate 是否已加载某个属性?
我需要一个帮助程序来了解是否已加载属性,以避免 LazyInitializationException
。是否可以?
@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());
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());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上有两种方法。
要查明惰性属性是否已初始化,您可以调用
Hibernate.isPropertyInitialized()
方法,以实体实例和属性名称作为参数。要查明惰性集合(或实体)是否已初始化(如您的示例中所示),您可以调用
Hibernate.isInitialized()
以集合(实体)实例作为参数。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.根据 Hibernate 5.4 的 文档
休眠 API
JPA
According to the documentation for Hibernate 5.4
Hibernate API
JPA