关于Hibernate的OpenSessionInView的问题
Struts2+Spring3+Hibernate3
异常:org.hibernate.HibernateException: createCriteria is not valid without active transaction
--------------
hibernate.xml文件:
<bean id="hibernateProperties"
destroy-method="destroy"
/>
</property>
<property name="hibernateProperties">
<ref bean="hibernateProperties" />
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath: job/model/</value>
</list>
</property>
</bean>
==========
<bean id="dialetMapping"
ref="sessionFactory" />
</bean>
===========
<tx:annotation-driven transaction-manager="txManager"/>
---------------------------------
web.xml配置文件
<filter>
<filter-name>OpenSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
--------------------
说明:把SSH框架搭建好后,其实只需要在web.xml文件加一个filter就行。<prop key="hibernate.current_session_context_class">thread</prop>这个TAG不起作用,所以删掉或注释掉。
注意:在项目中使用Spring+Hibernate的时候,会开启OpenSessionInViewFilter来阻止延迟加载的错误,但是在我们开启OpenSessionInViewFilter这个过滤器的时候FlushMode就已经被默认设置为了MANUAL,如果FlushMode是MANUAL或NEVEL,在操作过程中 hibernate会将事务设置为readonly,所以在增加、删除或修改操作过程中会出现如下错误,只要在那个filter里面加上这段代码就OK了
<init-param>
<param-name>flushMode</param-name>
<param-value>AUTO</param-value>
</init-param>