2012-10-27 69 views
0

我有一個@Entity有:JSF和JPA與@ManyToMany和自定義轉換器:LazyInitializationException中

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

@ManyToOne 
private IssueScope scope; 

    //getter/setter 
} 

我使用自定義IssueScopeConverter直接使用IssueScopeF:selectItems的。該轉換器只需

  • 返回ID的方法getAsString
  • 返回一個新創建的對象IssueScope(與ID集)爲getAsObject

這不會引起任何問題(和多次使用@ManyToOnep:selectOneMenu以及類似組件,代碼如下:

<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。

回答