JSF2和x2B;Spring3和hibernate:Spring托管Bean(@Scope value=";view";)LazyInitializationException异常
我已经使用找到的代码创建了自定义spring作用域,它支持使用与JSF2@ViewScoped类似的JSF2和x2B;Spring3和hibernate:Spring托管Bean(@Scope value=";view";)LazyInitializationException异常,spring,hibernate,jsf-2,Spring,Hibernate,Jsf 2,我已经使用找到的代码创建了自定义spring作用域,它支持使用与JSF2@ViewScoped类似的view作用域spring托管bean 当用户按下提交按钮时,会发生以下情况: - Backbean AA (@Scope(value="view") calls injected @Service BB - @Service BB calls DAO class CC - CC is @Transactional(readOnly = true) and has sessionFactory
view
作用域spring托管bean
当用户按下提交按钮时,会发生以下情况:
- Backbean AA (@Scope(value="view") calls injected @Service BB
- @Service BB calls DAO class CC
- CC is @Transactional(readOnly = true) and has sessionFactory injected
private SessionFactory sessionFactory;
//inject sessionGactory
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
- in CC i set the @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
for functions such as save/delete/saveOrUpdate
与此类相关的Hibernate映射为:
RegUser.hbm.xml
如果我将bean范围设置为@scope(value='request')
我尝试将以下内容添加到web.xml
中,但没有任何帮助:
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
openSessionInViewFilter
org.springframework.orm.hibernate4.support.OpenSessionInViewFilter
openSessionInViewFilter
/*
我还尝试添加了
,但也没有效果
有人能指出我做错了什么以及我如何解决这个问题吗?在这里找到了解决方案: 我需要补充一点
<f:attribute name="collectionType" value="java.util.HashSet" />
所有
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="com.testApp" />
<context:annotation-config />
<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
<property name="scopes">
<map>
<entry key="view">
<bean class="com.testApp.util.ViewScope" />
</entry>
</map>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver">
</property>
<property name="url" value="jdbc:jtds:sqlserver://localhost:1433/devDB">
</property>
<property name="username" value="blabla"></property>
<property name="password" value="blabla"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">20</prop>
<prop key="hibernate.c3p0.idle_test_period">300</prop>
<prop key="hibernate.c3p0.timeout">1800</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
<prop key="c3p0.acquire_increment">1</prop>
<props>
</property>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
...
<!-- some other beans declared below -->
...
<!-- all the Mapping Resource -->
</beans>
failed to lazily initialize a collection, no session or session was closed
viewId=/page/regNewCust.xhtml
location=C:\repo\dev\WebRoot\nz\regNewCust.xhtml
phaseId=PROCESS_VALIDATIONS(3)
Caused by:
org.hibernate.LazyInitializationException - failed to lazily initialize a collection, no session or session was closed
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:394)
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<f:attribute name="collectionType" value="java.util.HashSet" />