org.springframework.dao.InvalidDataAccessApiUsageException错误

  今天调试BUG时调到使用HibernateTemplate的saveOrUpdate方法时,遇到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.

  网上查找的资料对该问题的解释如下:

  问题原因分析:

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

  摘自:http://www.tuicool.com/articles/N3QzMb

  另外查找到的解决方法是将FlushMode改AUTO,再调用saveOrUpdate()方法,目前这样就可以执行通过,示例如下:

  

1 this.hibernateTemplate.getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO); 
2 this.hibernateTemplate.saveOrUpdate(unreadEvent);
原文地址:https://www.cnblogs.com/yinghuanblog/p/4911698.html