1 十一月 15, 2017 10:13:36 上午 org.apache.struts2.dispatcher.Dispatcher error
2 严重: Exception occurred during processing request: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into Flu
3 shMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
4 org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session
5 into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.
6 at org.springframework.orm.hibernate4.HibernateTemplate.checkWriteOperationAllowed(HibernateTemplate.java:1128)
7 at org.springframework.orm.hibernate4.HibernateTemplate$16.doInHibernate(HibernateTemplate.java:685)
8 at org.springframework.orm.hibernate4.HibernateTemplate.doExecute(HibernateTemplate.java:341)
9 at org.springframework.orm.hibernate4.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:309)
10 at org.springframework.orm.hibernate4.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:682)
.
.
.

错误描述:由于没有使用事务,当hibernate持久化数据到数据库时,控制台报出该错误,按照错误描述,是hibernate的flushMode设置不正确,需要修改.

软件环境:Struts2-Spring Ioc-Hibernate4 DB:Oracle11g.

解决办法:1 使用事务;2 修改flushMode;

1 使用spring-tx 在service层中开启事务; 注意 基于注解的service层不能多次扫描 !

spring-service.xml

  <bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven/> <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="do*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="save*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="del*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="mod*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="ins*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="upd*" propagation="REQUIRED" rollback-for="Exception" />
<tx:method name="invoke" propagation="REQUIRES_NEW" rollback-for="Exception" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>

service层:

@Service("userService")
public class UserServiceImpl implements UserService { @Autowired
private UserDao userDao; @Transactional
public void addUser(SUserEntity user) {
...
}
...
}

问题解决.

2 在dao层修改flushMode: 建议修改为AUTO,主要还是使用事务.

@Repository("userDao")
public class UserDaoImpl extends HibernateDaoSupport implements UserDao { @Resource(name = "sessionFactory")
public void setSessionFactoryDI(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
  /*this.getSessionFactory().getCurrentSession().setFlushMode(FlushMode.AUTO);*/
}
public void saveUser(SUserEntity user) {
     Session session = null;
Transaction tr = null;
try {
session = HibernateSessionFactory.getSession();
tr = session.beginTransaction();
session.saveOrUpdate(t);
tr.commit();
HibernateSessionFactory.closeSesssion();
} catch (Exception e) {
e.printStackTrace();
tr.rollback();
}
}
...
}

参考: https://stackoverflow.com/questions/6810158/java-hibernate-write-operations-are-not-allowed-in-read-only-mode

    http://blog.csdn.net/looyo/article/details/6309136">http://blog.csdn.net/looyo/article/details/6309136

hibernate flushMode 错误的更多相关文章

  1. Hibernate常见错误整理

    Hibernate常见错误合集   1.错误:object references an unsaved transient instance - save the transient instance ...

  2. Hibernate注解错误之- org.hibernate.MappingException: Could not determine type for:

    Hibernate 注解 @OneToOne 时候,出现以下错误,经调试,发现 注解要么全部放在字段上,要么全部放在get方法上,不能混合使用! org.hibernate.MappingExcept ...

  3. Hibernate学习笔记--Hibernate框架错误集合及解决

    错误1:MappingException: Unknown entity解决方案 http://jingyan.baidu.com/article/e75aca8552761b142edac6cf.h ...

  4. Hibernate学习错误集锦-GenericJDBCException: could not execute statement

    初次使用Hibernate,进行junit测试,报如下错误. 原因:Hibernate帮我们管理主键了,我们不需要对主键赋值,并且主键是自增的.所以在数据库中,逐渐选项应当勾选 org.hiberna ...

  5. 【hibernate】错误:org.hibernate.HibernateException: identifier of an instance of com.agen.entity.Monthdetail was altered from xx to xx

    所报错误: org.hibernate.HibernateException: identifier of an instance of com.agen.entity.Monthdetail was ...

  6. Hibernate插入错误:GenericJDBCException: could not insert:

    数据库中一般不能建立user(表名为User)表,将User类改名,又一次建立映射,问题就能够解决 当然,还有还有一种情况.就是类中id类型错误.要设置为Integer型才干够设置自己主动增长,否则也 ...

  7. hibernate常见错误

    1.Hibernate: Could not synchronize database state with session 1.主键不是自动生成的,然后自己没手动设置.  2.插入的实体字段跟数据库 ...

  8. hibernate中错误笔记

    1.在写Student.hbm.xml 中, hibernate-mapping 中 指定类和数据库对应的表字段时,不小心将property写为properties,报错: ERROR: HHH000 ...

  9. Use of @OneToMany or @ManyToMany targeting an unmapped class:hibernate映射错误

    hibernate映射异常:Use of @OneToMany or @ManyToMany targeting an unmapped class 新建了PO以后,要把PO所在的目录加入到Hiber ...

随机推荐

  1. ServiceStack.Text / Newtonsoft.Json 两种json序列化性能比较

    JSON序列化现在应用非常多,尤其在前后端分离的情况下,平常大多数C#下都使用Newtonsoft.Json来操作,量少的情况下,还可以忽略,但量大的情况下就要考虑使用ServiceStack.Tex ...

  2. FPGA在电平接口领域的应用

    电子技术的发展,产生了各种各样的电平接口. TTL电平: TTL电平信号之所以被广泛使用,原因是因为:通常我们采用二进制来表示数据.而且规定,+5V等价于逻辑"1",0V等价于逻辑 ...

  3. 配置zabbix agent向多个server发送数据

    1.背景: agent 端:dba-test-hzj02  172.16.59.197 server端:172.16.59.197  ,172.16.59.98 2.方式: 配置多个server,se ...

  4. BitmapImage 读取内存流和显示图片

    FileStream filestream = File.OpenRead(@"C:\Users\Administrator\Desktop\queryHeaderImg.png" ...

  5. netconf选用秘钥登录

    #! /usr/bin/python2.7import ncclientfrom ncclient import managerwith manager.connect(\ host="19 ...

  6. 小白学Maven第一篇配置

    在百度上搜Maven进入官网,然后在进Download里面把apache-maven-3.5.0-bin.zip(记得不要下错)下载下来 然后进行安装 (前提你配置了Java如下图) Java配置: ...

  7. JS中的作用域以及全局变量的问题

    一. JS中的作用域 1.全局变量:函数外声明的变量,称为全部变量 局部变量:函数内部使用var声明的变量,称为局部变量在JS中,只有函数作用域,没有块级作用域!!!也就是说,if/for等有{}的结 ...

  8. AspectCore.Extension.Reflection : .NET Core反射扩展库

    在从零实现AOP的过程中,难免会需要大量反射相关的操作,虽然在.net 4.5+/.net core中反射的性能有了大幅的优化,但为了追求极致性能,自己实现了部分反射的替代方案,包括构造器调用.方法调 ...

  9. JavaScript中的this基本问题

    在函数中 this 到底取何值,是在函数真正被调用执行的时候确定下来的,函数定义的时候确定不了. 执行上下文环境 : **定义**:执行函数的时候,会产生一个上下文的对象,里面保存变量,函数声明和th ...

  10. win10 uwp MVVM 轻量框架

    如果在开发过程,遇到多个页面之间,需要传输信息,那么可能遇到设计的问题.如果因为一个页面内包含多个子页面和多个子页面之间的通信问题找不到一个好的解决方法,那么请看本文.如果因为ViewModel代码越 ...