org.springframework.dao.InvalidDataAccessApiUsageException

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. 
org.springframework.orm.hibernate3.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1186) 
org.springframework.orm.hibernate3.HibernateTemplate$12.doInHibernate(HibernateTemplate.java:696) 
org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419) 
org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374) 
org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:694) 
com.tangjie.dao.StudentDAO.save(StudentDAO.java:38) 
com.tangjie.model.StudentModel.save(StudentModel.java:22) 
com.tangjie.action.StudentAction.regist(StudentAction.java:45) 
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
java.lang.reflect.Method.invoke(Unknown Source) 
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction

 

 在项目中使用ssH的时候,会开启OpenSessionInViewFilter来阻止延迟加载的错误,但是在我们开启OpenSessionInViewFilter这个过滤器的时候FlushMode就已经被默认设置为了MANUAL,如果FlushMode是MANUAL或NEVEL,在操作过程中 hibernate会将事务设置为readonly,所以在增加、删除或修改操作过程中会出现如下错误

rg.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。采用下面的方式即可:代码如下: 在web.xml文件中添加如下代码:即可解决延迟加载与上面的错误信息

<!--
hibernate由于设置了延迟加载,在action中并没有真正地查询数据
当跳到jsp页面上要显示数据时,hibernate中的session却已经关闭,导致数据无法显示 该过滤器的作用就是可以避免这种情况
-->
<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>

 

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