我知道这个问题被问了很多,但奇怪的是我在使用 jsf 1.1 / richfaces 3 以及 hibernate 3.7 和 spring 2.5 时没有任何问题。
现在我将前端迁移到 jsf 2.1 / richfaces 4(hibernate 和 spring 保持不变),当我尝试访问在 hibernate 映射配置中标记为“lazy=true”的集合时,会出现以下异常:
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
相同的 xhtml 中被 a 引用;Afi
这是 xhtml 的一部分,它尝试根据主表的 ajax 请求访问集合:
<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>
...
这是我的 web.xml:
<?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>
...
这是我的 application-context.xml 的一部分:
...
<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>
...
访问Afi
实体的服务定义如下:
<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>
如果有人能对这个问题有所了解,我将不胜感激。我不知道是否与迁移问题有关,或者此配置是否存在问题,并且以某种方式被先前版本的 jsf 和richfaces“掩盖”。
我试图把任何相关信息放在这里,我认为这会有所帮助,不会让读者感到无聊和不知所措,但如果这里有什么遗漏,请告诉我。
提前致谢。