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 ...
随机推荐
- SQL中游标的用法
游标:是用来对表从上下每行循环取值,将值连接成为字符串.例子:对 pubs 数据库的dbo.titles 表.1.取得表中的总价格:select sum(price) from dbo.titles2 ...
- M-自适应宽高样式
1 绝对定位 position: absolute; top: 0px; bottom: 0px; left: 0px; width: 100%; overflow: hidden;
- KM算法的应用
HDU2255 模板 难度x HDU2282 思维 难度XXx HDU3722 模板 难度X HDU3395 模版 HDU1533 最小值模型 难度x HDU2853 HDU3 ...
- 关于java的自动拆装箱若干细节问题
一.首先需要了解的几个前提: 1.自动装箱过程是通过调用valueOf方法实现的(如Integer.valueOf(10)),而拆箱过程是通过调用包装器的 xxxValue方法实现的(如Integer ...
- DevOps之归纳总结
唠叨话 关于德语关我屁事的知识点,仅提供精华汇总,具体知识点细节,参考教程网址,如需帮助,请留言. DevOps归纳总结 <DevOps功能与性能>浏览器(饼干Cookie.会话Sessi ...
- java连接sqlserver2008r2 心得
现在是该轻松一笑的时候了,困扰已久的问题有了解释了. 之前的各种连不上,说到底还是权限问题,sqlserver2008r2的权限分得太细了. 两个实例间数据库互相都看不到,更不用谈访问了. 端口号也是 ...
- 【特效】hover向上翻转效果
前段时间做了很多有关css3动画的项目.虽然当时都较圆满的完成了,但事后还是要总结一下的,趁着近期工作不忙,系统的学习了一些动画理论,重点看了transform3D变换,学习了translate平移. ...
- 在SQL Server中实现关系模型的阶梯到级别3的t -SQL DML
在SQL Server中实现关系模型的阶梯到级别3的t -SQL DML 格雷戈里·拉森(Gregory Larsen),2017/08/02(第一次出版:2011 /11/09) 原文链接:http ...
- Eratosthenes,筛法求素数
//筛法求区间[0,n]的所有素数,v为素数表 //v[i]==0,i为素数 void f(int n) { int m=sqrt(n+0.5); memset(v,,sizeof(v)); ;i&l ...
- Oracle学习笔记之游标详解
游标 游标存在意义:解决"select *"返回空.多行记录问题,但凡select,就可能多行结果集,也就需要用游标. 游标分4步走:cursor.open.fetch.close ...