繁体   English   中英

具有@ManyToMany和自定义转换器的JSF和JPA:LazyInitializationException

[英]JSF and JPA with @ManyToMany and custom converter: LazyInitializationException

我有一个@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也将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未按下按钮后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()从未输入。

该问题似乎与使用JSF Converter(指集合)时的延迟加载异常有关,但是我不使用Seam持久性或具有特殊的TransactionInterceptor`。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM