简体   繁体   English

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

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

I have an @Entity with : 我有一个@Entity

@Entity
public class Issue implements Serializable
{
  @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
 protected long id; 

 @ManyToOne
 private IssueScope scope;

  //getter/setter
}

I use the custom IssueScopeConverter to directly use the IssueScope with f:selectItems . 我使用自定义的IssueScopeConverter直接将IssueScopef:selectItems结合使用 The converter simply 转换器简单

  • returns the id for the method getAsString 返回方法getAsStringID
  • return a newly created object IssueScope (with id set) for getAsObject getAsObject返回一个新创建的对象IssueScope (设置了ID

This does not raise any problems (and is used many times before with @ManyToOne ) with p:selectOneMenu and similar components with code like this: 这不会对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>

Now let's describe my problem: In fact I don't need a @ManyToOne , I need a @ManyToMany relationship from Issue to IssueScope : 现在让我们描述我的问题:实际上,我不需要@ManyToOne ,我需要从IssueIssueScope@ManyToMany关系:

@ManyToMany(fetch=FetchType.EAGER)
private List<IssueScope> scopes;

and the XHTML will change to this: 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>

If I newly create Issue and then push the Save button to persist the entity this is done without exception. 如果我是新创建的Issue ,然后按“ 保存”按钮以保留实体,则不会发生异常。 Even selected IssueScopes are persisted. 甚至选定的IssueScopes也将IssueScopes Then, if I want to update the entity, I get a 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 after pushing the button. 然后,如果我要更新实体,则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

The method public void save() in my @Named @ViewScoped IssueBean is never entered. 我的@Named @ViewScoped IssueBean的方法public void save()从未输入。

The problem seems to be related to Lazy loading exception when using JSF Converter (refering to a collection) , but I don't use Seam persistence or have special kind of TransactionInterceptor`. 该问题似乎与使用JSF Converter(指集合)时的延迟加载异常有关,但是我不使用Seam持久性或具有特殊的TransactionInterceptor`。

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

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