在支持 beanedit()方法中,我有以下代码
public void edit(Entity e) {
this.entity = entityService.find(e);
this.entity.setLazyList(new ArrayList(this.entity.getLazyList()));
}
public Entity getEntity() {
return this.entity;
}
如图所示,我通过用自己的ArrayList. 奇怪的是,内部惰性列表一存在方法PersistentBag就会返回edit()。我做了什么来发现这种行为:
- 在编辑方法中放置一个断点。找到后,我看到已创建实体的 JVM ID,说它是 101。
- 在方法中设置一个断点
getEntity()。edit()存在后被getEntity()调用,实体的JVM ID还是101,但是entity.lazyList有PersistentBag而不是ArrayList。
我的 LazyList 绑定到 . 然后,当我在 JSF 页面中按下保存按钮时,此 PersistentBag 会导致 LazyInitializationException。抛出异常(在调用支持 bean 中的 save() 方法之前)因为来自 Mojarra 的这一部分(也用 MyFaces 测试,它也抛出异常):
菜单渲染器,行:365
// No cloned instance so if the modelType happens to represent a
// concrete type (probably not the norm) try to reflect a
// no-argument constructor and invoke if available.
if (targetCollection == null) {
//noinspection unchecked
targetCollection =
createCollection(currentValue, modelType);
}
由于currentValue不幸的是 PersistentBag(并且也已初始化),因此 createCollection() 返回此对象的新实例,该实例不再知道会话和加载的对象。