Spring org.hibernate.LazyInitializationException将webapp迁移到JSF 2.1&;RichFaces 4
我知道这个问题被问了很多,但奇怪的是,我在使用JSF1.1/RichFaces3以及Hibernate3.7和Spring2.5时没有遇到任何问题 现在,我将前端迁移到jsf 2.1/richfaces 4(hibernate和spring保持不变),当我尝试访问hibernate映射配置中标记为“lazy=true”的集合时,会出现以下异常:Spring org.hibernate.LazyInitializationException将webapp迁移到JSF 2.1&;RichFaces 4,spring,hibernate,jsf,transactions,richfaces,Spring,Hibernate,Jsf,Transactions,Richfaces,我知道这个问题被问了很多,但奇怪的是,我在使用JSF1.1/RichFaces3以及Hibernate3.7和Spring2.5时没有遇到任何问题 现在,我将前端迁移到jsf 2.1/richfaces 4(hibernate和spring保持不变),当我尝试访问hibernate映射配置中标记为“lazy=true”的集合时,会出现以下异常: org.hibernate.LazyInitializationException: failed to lazily initialize a col
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.mu.afs.Afi.cods, no session or session was closed
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:358)
at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
at org.hibernate.collection.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:97)
at org.hibernate.collection.PersistentBag.size(PersistentBag.java:225)
at javax.faces.model.ListDataModel.isRowAvailable(ListDataModel.java:91)
at javax.faces.model.ListDataModel.setRowIndex(ListDataModel.java:105)
我试图访问的集合由rich:datatable
引用,它与Afi
实体位于同一个xhtml中;
下面是xhtml的一部分,它尝试在主表的ajax请求时访问collecion:
<rich:extendedDataTable id="tableCods" selectionMode="single"
selection="#{afiBean.codSelected}"
style="width: 100%; height: 100px"
value="#{afiBean.selectedAfi.cods}" var="cod">
...
<rich:column width="50px" style="text-align: right;">
<f:facet name="header"><h:outputText value="Months"/></f:facet>
<h:outputText value="#{cod.months}" />
</rich:column>
<rich:column width="80px" style="text-align: right;">
<f:facet name="header"><h:outputText value="Cap" /> </f:facet>
<h:outputText value="#{cod.cap}">
<f:convertNumber pattern="######,##0.00" locale="es_AR" />
</h:outputText>
</rich:column>
<rich:column width="200px" style="text-align: center;">
<f:facet name="header"><h:outputText value="Class"/></f:facet>
<h:outputText value="#{cod.clazz}" />
</rich:column>
...
如果有人能解释一下这个问题,我将不胜感激。我不知道这是否与迁移问题有关,也不知道这个配置是否有问题,是否被jsf和richfaces的早期版本“掩盖”了
我试着把我认为会有帮助的相关信息放在这里,而不会让读者感到无聊和不知所措,但是如果这里缺少什么,请告诉我
提前感谢。您的意思是说jsf 1.1/richafaces 3不会引发lazyinitexception,当您升级到jsf 2.1/richfaces 4时,您会得到这个例外吗?你能发布相关的jsf代码来表示正在使用的组件吗。我用访问集合的xhtml部分更新了这个问题。在我看来,richfaces扩展数据表添加了一些额外的调用,如果您参考richfaces更改日志,您可能会找到一些解决方案。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
...
<filter>
<filter-name>sessionFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sessionFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
...
...
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="txProxyTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="txManager" />
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="store*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED</prop>
<prop key="read*">PROPAGATION_REQUIRED</prop>
<prop key="load*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
...
<bean id="afiService" parent="txProxyTemplate">
<property name="proxyInterfaces" value="com.mu.afs.services.AfiService" />
<property name="target">
<bean class="com.mu.afs.services.impl.AfiServiceImpl">
<property name="afiDao" ref="afiDao" />
</bean>
</property>
</bean>
<bean id="afiliadosDao"
class="com.mu.afs.dao.impl.HibernateAfiDaoImpl"
parent="hibernateDaoSupport">
</bean>