【发布时间】:2012-10-17 15:47:41
【问题描述】:
我有一个@Entity :
@Entity
public class Issue implements Serializable
{
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
protected long id;
@ManyToOne
private IssueScope scope;
//getter/setter
}
我使用自定义的IssueScopeConverter 直接将IssueScope 与f:selectItems 一起使用。转换器很简单
- 返回方法
getAsString的id - 为
getAsObject返回一个新创建的对象IssueScope(设置了id)
对于 p:selectOneMenu 和类似的组件,这不会引起任何问题(并且之前与 @ManyToOne 一起使用过很多次),代码如下:
<h:form id="fScope">
<p:selectOneButton rendered="true" value="#{issueBean.issue.scope}"
converter="IssueScopeConverter">
<f:selectItems value="#{issueBean.issueScopes}" var="s"
itemLabel="#{s.name}" itemValue="#{s}"/>
</p:selectOneButton>
<p:commandButton value="Save" actionListener="#{issueBean.save()}"/>
</h:form>
现在让我们描述一下我的问题:实际上我不需要@ManyToOne,我需要从Issue 到IssueScope 的@ManyToMany 关系:
@ManyToMany(fetch=FetchType.EAGER)
private List<IssueScope> scopes;
XHTML 会变成这样:
<h:form id="fScopes">
<p:selectManyCheckbox value="#{issueBean.issue.scopes}"
converter="ErpIssueScopeConverter">
<f:selectItems value="#{issueBean.issueScopes}" var="s"
itemLabel="#{s.name}" itemValue="#{s}"/>
</p:selectManyCheckbox>
<p:commandButton value="Save" actionListener="#{issueBean.save()}"/>
</h:form>
如果我新创建了Issue,然后按下保存 按钮来保存实体,这将毫无例外地完成。甚至选择的IssueScopes 也会被保留。然后,如果我想更新实体,我会在按下按钮后得到failed to lazily initialize a collection, no session or session was closed: org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed。
我的@Named @ViewScoped IssueBean 中的方法public void save() 从未输入过。
问题似乎与Lazy loading exception when using JSF Converter (refering to a collection) 有关,但我没有使用Seam 持久性或有特殊类型的TransactionInterceptor`。
【问题讨论】:
标签: java jpa jsf-2 many-to-many