2012-02-03 63 views
2

我有以下實體繼承,讓我們從根開始(它只是一個帶有自動遞增整數ID表):休眠4:LazyInitializationException中的EL表達式

@Entity 
@Table(name = "Contacts") 
@Inheritance(strategy = InheritanceType.JOINED) 
@DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.STRING) 
public abstract class Contact implements Serializable 
{ 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column 
    protected Integer id; 

    ... 
} 

OrganizationalUnit只是一個名字加一個繼承的ID:

@Entity 
@Table(name = "OrganizationalUnits") 
public abstract class OrganizationalUnit extends Contact 
{ 
    @Column 
    protected String name; 

    ... 
} 

Organization只是一個空實體(被引用):

@Entity 
@Table(name = "Organizations") 
public abstract class Organization extends OrganizationalUnit 
{ 
    ... 
} 

最後但並非最不重要的:

@Entity 
@Table(name = "Companies") 
@DiscriminatorValue(value = "company") 
public class Company extends Organization 
{ 
    @Basic(optional = false) 
    @Column(name = "dnd_type") 
    private String dndType; 

    ... 
} 

本公司因有3列/字段:ID,名稱和DND類型。

每家公司都來自系統中的一個文件的實體(每個文檔具有擁有公司)引用:

@Entity 
@Table(name = "Documents") 
@Inheritance(strategy = InheritanceType.JOINED) 
@DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.STRING) 
public abstract class Document implements Serializable 
{ 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column 
    protected Integer id; 

    @Basic(optional = false) 
    @Column(name = "file_name") 
    protected String fileName; 

    @ManyToOne(fetch = FetchType.LAZY, optional = false) 
    @JoinColumn(name = "company_id", referencedColumnName = "id") 
    protected Company company; 

    ... 
} 

注意@ManyToOne(fetch = FetchType.LAZY, optional = false)對公司的關係。 這是強制性不要有LAZY這裏,使用預先抓取此處鍵入修復我談論這裏問題(,但我想它懶工作,也因爲我使用一個代碼生成器重新生成實體類...它有工作

我引用CompanydndType在JSF頁面PQ-edit.xhtml每個文檔喜歡!

<ui:include src="/subviews/repo-filename-dnd-panel.xhtml"> 
    <ui:param name="document" value="#{doc}" /> 
    <ui:param name="documentFileName" value="#{doc.fileName}" /> 
    <ui:param name="documentCompanyName" value="#{doc.company.name}" /> 
    <ui:param name="documentCompanyDndSuffix" value="#{doc.company.dndType}" /> <!-- EXCEPTION HERE --> 
</ui:include> 

這些只是傳遞給子視圖/subviews/repo-filename-dnd-panel.xhtml,在這裏我只是參考的參數,可以如documentCompanyDndSuffix,來構造一些HTML。

我得到#{doc.company.dndType}現在是:

13:53:56,043 SEVERE [org.richfaces.log.Context] (http--127.0.0.1-8080-3) /subviews/repo-filename-dnd-panel.xhtml @14,63 type="doc-#{documentCompanyDndSuffix}": /pq-edit.xhtml @139,94 value="#{doc.company.dndType}": org.hibernate.LazyInitializationException: could not initialize proxy - no Session: javax.el.ELException: /subviews/repo-filename-dnd-panel.xhtml @14,63 type="doc-#{documentCompanyDndSuffix}": /pq-edit.xhtml @139,94 value="#{doc.company.dndType}": org.hibernate.LazyInitializationException: could not initialize proxy - no Session 
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT] 
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1] 
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1] 
    at org.richfaces.component.UIDragSource.getType(UIDragSource.java:95) [richfaces-components-ui-4.1.0.Final.jar:] 
    at org.richfaces.renderkit.DragSourceRenderer.getOptions(DragSourceRenderer.java:55) [richfaces-components-ui-4.1.0.Final.jar:] 
    at org.richfaces.renderkit.DnDRenderBase.buildClientScript(DnDRenderBase.java:66) [richfaces-components-ui-4.1.0.Final.jar:] 
    at org.richfaces.renderkit.DnDRenderBase.buildAndStoreScript(DnDRenderBase.java:43) [richfaces-components-ui-4.1.0.Final.jar:] 
    at org.richfaces.renderkit.DnDRenderBase.doEncodeEnd(DnDRenderBase.java:74) [richfaces-components-ui-4.1.0.Final.jar:] 
    at org.richfaces.renderkit.RendererBase.encodeEnd(RendererBase.java:175) [richfaces-components-ui-4.1.0.Final.jar:] 
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1] 
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1763) [jboss-jsf-api_2.1_spec-2.0.0.Beta1.jar:2.0.0.Beta1] 
    at org.richfaces.renderkit.RendererBase.renderChildren(RendererBase.java:276) [richfaces-components-ui-4.1.0.Final.jar:] 
    at org.richfaces.renderkit.html.AjaxOutputPanelRenderer.encodeChildren(AjaxOutputPanelRenderer.java:57) [richfaces-components-ui-4.1.0.Final.jar:] 
. 
. lots of RichFaces and JSF stuff 
. 
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:626) [jbossweb-7.0.7.Final.jar:] 
    at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:2033) [jbossweb-7.0.7.Final.jar:] 
    at java.lang.Thread.run(Unknown Source) [:1.7.0_02] 
Caused by: org.hibernate.LazyInitializationException: could not initialize proxy - no Session 
    at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:149) [hibernate-core-4.0.0.Final.jar:4.0.0.Final] 
    at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:195) [hibernate-core-4.0.0.Final.jar:4.0.0.Final] 
    at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185) [hibernate-core-4.0.0.Final.jar:4.0.0.Final] 
    at de.poyry.pqgenerator.model.Company_$$_javassist_22.getDndType(Company_$$_javassist_22.java) [classes:] 
    at sun.reflect.GeneratedMethodAccessor457.invoke(Unknown Source) [:1.7.0_02] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) [:1.7.0_02] 
    at java.lang.reflect.Method.invoke(Unknown Source) [:1.7.0_02] 
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:302) [jboss-el-api_2.2_spec-1.0.0.Final.jar:1.0.0.Final] 
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT] 
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT] 
    at org.apache.el.parser.AstValue.getValue(AstValue.java:169) [jbossweb-7.0.7.Final.jar:] 
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189) [jbossweb-7.0.7.Final.jar:] 
    at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50) [weld-core-1.1.4.Final.jar:2011-11-22 20:01] 
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT] 
    at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:67) [jbossweb-7.0.7.Final.jar:] 
    at org.apache.el.parser.AstDeferredExpression.getValue(AstDeferredExpression.java:44) [jbossweb-7.0.7.Final.jar:] 
    at org.apache.el.parser.AstCompositeExpression.getValue(AstCompositeExpression.java:50) [jbossweb-7.0.7.Final.jar:] 
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189) [jbossweb-7.0.7.Final.jar:] 
    at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50) [weld-core-1.1.4.Final.jar:2011-11-22 20:01] 
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109) [jsf-impl-2.1.5-jbossorg-1.jar:2.1.5-SNAPSHOT] 
    ... 72 more 

我不知道爲什麼我在這裏得到LazyInitializationException中,考慮到公司實體不暗示懶惰休眠4和以前調用#{doc.company.name}作品精細。

更新:

我最近測試了在GlassFish 3.1.2所有的罰款。所以我開始相信這是某種JBoss AS 7或JBoss AS事務管理問題/策略不匹配。

我的假設是公司名稱在某個事務處理過程中被訪問和取消處理,並且在實體管理器關閉後,dndType被訪問,但尚未未被取消(而name已被刪除)......它在我看來像EJB事務處理的問題(其中我確實還是有赤字)...

我會在這裏很快添加更多的信息。

+0

您使用什麼交易策略?似乎沒有JPA會話處於活動狀態,因此消息:'無法初始化代理 - 沒有會話。你可能會分享你的持久性配置嗎? – siebz0r 2012-06-03 12:10:46

+0

糟糕,我前段時間解決了這個問題。看到我的答案。 – Kawu 2012-06-03 12:56:56

回答

0

的問題是,該公司未取在聲明中 - 就這麼簡單。我只是在一個交易仍然活躍類似的情況,偶爾運氣,所以這一塊似乎是失敗的,但其實別人也知道。

我沒有意識到,從JSF頁面訪問時,從DB獲取文檔列表的方法已經運行並且事務已經消失。

+0

我面臨着同樣的問題,我得到lazyInitialization異常,雖然參考被宣佈爲渴望獲取。您可以提供關於解決方案的更多有用的詳細信息,請致電 。 – Genjuro 2013-07-19 10:07:07

+0

在JPQL中使用'JOIN FETCH'。 – Kawu 2013-07-19 13:06:51

0

你應該試試這個: fetch = FetchType.EAGER 它解決了我的問題。