为了解决Hibernate延迟加载问题,我在web.xml中加入了OpenSessionInViewFilter配置
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter><filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!----><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><!----> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath*:spring/*.properties</value> </list> </property> </bean> <!----> <aop:config proxy-target-class="true"> <aop:advisor pointcut="execution(* com.zx.blog.*.service.impl.*Service*.*(..))" advice-ref="txAdvice"> </aop:config> <!----> <tx:advice id="txAdvice"> <tx:attributes> <tx:method name="save*"> <tx:method name="update*"> <tx:method name="remove*"> <tx:method name="delete*"> <tx:method name="get*" read-only="true"> <tx:method name="load*" read-only="true"> <tx:method name="find*" read-only="true"> </tx:attributes> </tx:advice>
</beans>
很明显,你事务配置没有做好。使用了这个过滤器后,其工作是这样的,请求到过滤器后,打开Session,但是此时是只读模式,只能读,不能写。遇到Spring的带事务的Bean的时候,根据事务管理器的定义,确定是否转换为读写模式。当这个事务处理完以后,再次转回只读模式。
从你的错误上看,就是你在只读模式下进行了写操作,所以就出错了。
你查看一下你的Spring事务配置.
检查下get* load* find* 的方法
有设置成只读事务的方法包含了写操作
这个跟OpenSessionInView没有关系,是事务配置的问题。
下面的是我正用的,可以参考一下:
[code="xml"]
<aop:config proxy-target-class="true">
<aop:advisor pointcut="execution(* com.systop..service..*Manager.*(..))"
advice-ref="txAdvice"/>
</aop:config>
<!-- 基本事务定义,使用transactionManager作事务管理 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="select*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="pageQuery*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
[/code]