org.springframework.dao.InvalidDataAccessApiUsageException 异常

org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.

在使用hibernate 的 HibernateTemplate的保存方法时出现该异常,
本人经过研究与查资料找到的两种解决办法如下:

1、在调用保存方法前将flushMode改为auto

this.ht.getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO);


2、修改web.xml中spring的过滤器:OpenSessionInViewFilter参数如下

	<filter>
		<filter-name>OpenSessionInViewFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
          <init-param>  
               <param-name>flushMode</param-name>  
               <param-value>AUTO</param-value>  
           </init-param>
	</filter>
	<filter-mapping>
		<filter-name>OpenSessionInViewFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

你可能感兴趣的:(Hibernate,异常)