我遇到了JPA CascadeType.REFRESH属性的奇怪问题。
我有一个简单的父子关系,其中在父域对象(LineEquipmentFormat)中添加了如下的层叠属性
OneToMany(cascade = { CascadeType.REFRESH, CascadeType.MERGE, CascadeType.PERSIST,
CascadeType.REMOVE}, mappedBy = "lineEquipmentFormat")
public List<LineEquipmentFormatDivision> getLineEquipmentFormatDivisions() {
return lineEquipmentFormatDivisions;
}
但是,当我从数据库中获取父LineEquipmentFormat对象时,没有得到LineEquipmentFormatDivisions的列表。
我得到的错误是
17:46:34,251 ERROR [LazyInitializationException] failed to lazily initialize a collection of role: LineEquipmentFormat.lineEquipmentFormatDivisions, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: LineEquipmentFormat.lineEquipmentFormatDivisions, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:380)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:372)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:365)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)
at org.hibernate.collection.PersistentBag.toString(PersistentBag.java:506)
我正在使用Jboos 5.1和Oracle 10G。
请提供更多有关此错误可能原因的输入。
谢谢
最佳答案
当您将级联类型定义为刷新时,这意味着在父LineEquipmentFormat上调用entityManager.refresh()时,也将在子LineEquipmentFormatDivisions上调用它。但是,您收到的错误指示您在会话关闭时尝试延迟初始化集合。如果在会话关闭后需要此集合,则需要通过设置fetchType急切地获取它们。