hibernate flushMode 错误
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();
}
}
...
}
hibernate flushMode 错误的更多相关文章
- Hibernate常见错误整理
Hibernate常见错误合集 1.错误:object references an unsaved transient instance - save the transient instance ...
- Hibernate注解错误之- org.hibernate.MappingException: Could not determine type for:
Hibernate 注解 @OneToOne 时候,出现以下错误,经调试,发现 注解要么全部放在字段上,要么全部放在get方法上,不能混合使用! org.hibernate.MappingExcept ...
- Hibernate学习笔记--Hibernate框架错误集合及解决
错误1:MappingException: Unknown entity解决方案 http://jingyan.baidu.com/article/e75aca8552761b142edac6cf.h ...
- Hibernate学习错误集锦-GenericJDBCException: could not execute statement
初次使用Hibernate,进行junit测试,报如下错误. 原因:Hibernate帮我们管理主键了,我们不需要对主键赋值,并且主键是自增的.所以在数据库中,逐渐选项应当勾选 org.hiberna ...
- 【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 ...
- Hibernate插入错误:GenericJDBCException: could not insert:
数据库中一般不能建立user(表名为User)表,将User类改名,又一次建立映射,问题就能够解决 当然,还有还有一种情况.就是类中id类型错误.要设置为Integer型才干够设置自己主动增长,否则也 ...
- hibernate常见错误
1.Hibernate: Could not synchronize database state with session 1.主键不是自动生成的,然后自己没手动设置. 2.插入的实体字段跟数据库 ...
- hibernate中错误笔记
1.在写Student.hbm.xml 中, hibernate-mapping 中 指定类和数据库对应的表字段时,不小心将property写为properties,报错: ERROR: HHH000 ...
- Use of @OneToMany or @ManyToMany targeting an unmapped class:hibernate映射错误
hibernate映射异常:Use of @OneToMany or @ManyToMany targeting an unmapped class 新建了PO以后,要把PO所在的目录加入到Hiber ...
随机推荐
- Jquery地图热点效果-鼠标经过弹出提示信息
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- javascript-OOP基础详解
前 言 S N 今天给大家详解一下面向对象编程(简称OOP)基础,OOP 语言使我们有能力定义自己的对象和变量类型 .对象拥有属性和方法 . 所以今天就给大家详解对象和类 . 1-1简介 ...
- java 静态代码块 代码块 构造函数 静态成员变量 成员变量的初始化顺序
没有父类的情况 1,静态代码块-->静态成员变量-->成员变量(实例属性)-->代码块-->构造函数 2, 静态代码块 和 静态成员变量 只会初始化一次 有父类的情况 1,父类 ...
- 03Vue事件
Vue提供了事件的绑定,方法写在methods对象中. 绑定dom中有两种方法: 方法一:v-on:click/dblclcick/mouseOver/mouseOut="方法名" ...
- python实现进度条和百分比同时显示
python中同时打印进度条和百分比 仅打印进度条: import sys,time for i in range(100): sys.stdout.write('>') sys.stdout. ...
- Appium python自动化测试系列之自动化截图(十一)
11.1 截图函数的正常使用 11.1.1 截图方法 无论是在手动测试还是自动化测试中场景复现永远是一个很重要的事情,有时候一些问题可能很难复现,这个都需要测试人员对bug有很高的敏感度,在一般的情况 ...
- Java基础笔记3
控制语句 1. if语句 if(条件){ //如果条件成立,则运行该大括号内的内容. } if(条件){ //如果条件成立,则运行该大括号内的内容. }else{ //如果条件不成立,则运行该大括号内 ...
- python自动化运维七:fabric
p { margin-bottom: 0.25cm; line-height: 120% } a:link { } p { margin-bottom: 0.25cm; line-height: 12 ...
- 使用bower init创建bower.json文件
使用bower init 可以快速创建bower.json文件 bower init 回答一系列问题后就可以了,其中大部分问题可以按enter跳过.
- 使用Identity Server 4建立Authorization Server (1)
预备知识: http://www.cnblogs.com/cgzl/p/7746496.html 本文内容基本完全来自于Identity Server 4官方文档: https://identitys ...