我得到了javax.el.ELException org.hibernate.LazyInitializationException:无法初始化代理-没有会话
当我想在XHTM页面中打印我的formation对象的Other对象属性的属性时,我得到了一个异常,即存在我得到了javax.el.ELException org.hibernate.LazyInitializationException:无法初始化代理-没有会话,hibernate,primefaces,Hibernate,Primefaces,当我想在XHTM页面中打印我的formation对象的Other对象属性的属性时,我得到了一个异常,即存在javax.el.ELException异常:读取类型模型上的'intitule'时出错。Theme,这是我的代码: <h:form id="a"> <p:growl id="growl" showDetail="true" sticky="false" />
javax.el.ELException异常:读取类型模型上的'intitule'时出错。Theme
,这是我的代码:
<h:form id="a">
<p:growl id="growl" showDetail="true" sticky="false" />
<p:dataTable var="formationObject"
value="#{formationBean.listformation}" id="AjoutTab"
widgetVar="FormationTable" emptyMessage="Formation non trouvé"
rows="15"
style="width:500px;font-size:13px;margin-left: 0px">
<f:facet name="header">
<p:outputPanel>
<h:outputText value="Recherche:" />
<p:inputText id="globalFilter"
onkeyup="FormationTable.filter()"
style="width:80px ;float:center " />
</p:outputPanel>
</f:facet>
<p:column headerText="Intitule " id="formationRef"
filterBy="#{formationObject.planning}" filterMatchMode=""
footerText=" reférence exacte" width="15px">
<h:outputText value="#{formationObject.planning.intitule}" />
</p:column>
<p:column style="width:4%">
<p:commandButton value="Analyser"
icon="ui-icon-search"
action="#{formationBean.redirectModification()}"
ajax="false" title="Analyser" />
</p:column>
</p:dataTable>
</h:form>
LazyInitializationException
是一个非常常见的错误,它实际上总是意味着您要么不完全理解hibernate代理,要么无法控制应用程序中发生的情况
当您使用@OneToMany注释时,hibernate通常使用延迟加载。这意味着,在对象中,您有一个代理来代替集合,它不包含任何元素,并在第一次请求时加载元素(例如get()
或size()
)
但是,如果在事务性
范围之外访问集合(在web应用程序中通常意味着EL方法),则绑定到代理的hibernate会话将不再存在
要防止此类行为,您可以选择两种方式:
1) 不要使用@OneToMany
。相反,如果您想要一个集合,请提供一个用于加载该集合的DAO方法
2) 请确保从不从DAO方法返回延迟加载代理的对象。您可以遍历集合,将其设置为null,或者通过映射器(如Dozer)传递DTO,该映射器将调用所有getter并遍历所有集合,返回没有代理的对象。您还可以在hibernate会话上调用execute()
,但通过迭代或设置null,您可以知道对象是否已加载