我有以下實體繼承,讓我們從根開始(它只是一個帶有自動遞增整數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這裏,使用預先抓取此處鍵入修復我談論這裏問題(,但我想它懶工作,也因爲我使用一個代碼生成器重新生成實體類...它有工作)
我引用Company
的dndType
在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事務處理的問題(其中我確實還是有赤字)...
我會在這裏很快添加更多的信息。
您使用什麼交易策略?似乎沒有JPA會話處於活動狀態,因此消息:'無法初始化代理 - 沒有會話。你可能會分享你的持久性配置嗎? – siebz0r 2012-06-03 12:10:46
糟糕,我前段時間解決了這個問題。看到我的答案。 – Kawu 2012-06-03 12:56:56