session OpenSessionInViewFilter

  •  
    1. org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.   
    2.     at org.springframework.orm.hibernate3.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1186)   
    3.     at org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:696)   
    4.     at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)   
    5.  .............................(代码偏长,余下的省略掉)  

 

在项目中使用Spring+Hibernate的时候,会开启OpenSessionInViewFilter来阻止延迟加载的错误,但是在我们开启OpenSessionInViewFilter这个过滤器的时候FlushMode就已经被默认设置为了MANUAL,如果FlushMode是MANUAL或NEVEL,在操作过程中 hibernate会将事务设置为readonly,所以在增加、删除或修改操作过程中会出现如下错误
org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER) - turn your Session into FlushMode.AUTO or remove 'readOnly' marker from transaction definition;
解决办法:直接修改OpenSessionInViewFilter过滤器的配置,配置过滤器的时候配置就是在一般的配置里面加上下面部分就可以了,直接指定flushMode的配置就OK了:


  

  1. <filter>  
  2.        <filter-name>hibernateFilter</filter-name>  

  1.        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>  
  2.       [<init-param>  
  3.            <param-name>flushMode</param-name>  
  4.            <param-value>AUTO</param-value>  
  5.        </init-param>
  6.        <init-param>  
  7.            <param-name>singleSession</param-name>  
  8.            <param-value>true</param-value>  
  9.        </init-param>  
  10.    </filter>  
  11.    <filter-mapping>  
  12.        <filter-name>hibernateFilter</filter-name>  
  13.        <url-pattern>/*</url-pattern>  
  14.    </filter-mapping>  

你可能感兴趣的:(DAO,spring,Hibernate,orm)