关于Hibernate的OpenSessionInView的问题
Struts2+Spring3+Hibernate3
异常:org.hibernate.HibernateException:createCriteriaisnotvalidwithoutactivetransaction
--------------
hibernate.xml文件:
<beanid="hibernateProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<propertyname="properties"><props>
<propkey="hibernate.query.substitutions">true'T',false'F'</prop>
<propkey="hibernate.show_sql">false</prop>
<propkey="hibernate.max_fetch_depth">1</prop>
<propkey="hibernate.jdbc.batch_size">3</prop>
<propkey="hibernate.order_updates">true</prop>
<propkey="hibernate.max_fetch_depth">1</prop>
<propkey="hibernate.use_get_generated_keys">true</prop>
<propkey="hibernate.generate_statistics">false</prop>
<propkey="hibernate.cache.use_structured_entries">false</prop>
<propkey="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider
</prop>
<propkey="hibernate.cache.use_second_level_cache">false</prop>
<propkey="hibernate.cache.use_query_cache">false</prop>
<!--
<propkey="hibernate.current_session_context_class">thread</prop>
-->
</property>
</bean>
==========
<beanid="sessionFactory"destroy-method="destroy"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="dataSource">
<refbean="connectionFactory"/>
</property>
<propertyname="hibernateProperties">
<refbean="hibernateProperties"/>
</property>
<propertyname="mappingDirectoryLocations">
<list>
<value>classpath:job/model/</value>
</list>
</property>
</bean>
==========
<beanid="dialetMapping"
class="com.util.mapping.CaseInsensitiveKeyValueMapping">
<propertyname="mapping">
<map>
<entrykey="oracle">
<value>com.util.hibernate.Oracle9Dialect</value>
</entry>
</map>
</property>
</bean>
===========
<beanid="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory"ref="sessionFactory"/>
</bean>
===========
<tx:annotation-driventransaction-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就行。<propkey="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>