【问题标题】:JSF and JPA with @ManyToMany and custom converter: LazyInitializationException带有 @ManyToMany 和自定义转换器的 JSF 和 JPA:LazyInitializationException
【发布时间】: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 直接将IssueScopef:selectItems 一起使用。转换器很简单

  • 返回方法getAsStringid
  • 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,我需要从IssueIssueScope@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


    【解决方案1】:

    【讨论】:

    • 链接断开,站点 simtay 不再存在...
    猜你喜欢
    • 2012-12-16
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 2013-10-11
    相关资源
    最近更新 更多