hibernate a different object with the same identifier value was already associated with the session
来源:互联网 发布:淘宝上怎么买弓弩 编辑:程序博客网 时间:2024/05/16 19:49
问题情况:使用hibernate来进行对对象的save、update操作时,出现了exception,导致数据保存不成功,具体报错如是:
Exception in thread "main" org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:
在hibernate中同一个session里面有了两个相同标识但是是不同实体。
楼主遇到的使用情况是 在同一个方法中,对数据库同一张表进行了多次session.update()操作;
解决方案: 将session.update(object c)或session.saveOrUpdate(object c)方法替换为session.merge (object c);
session.merge()方法作用:
新new一个对象,如果该对象设置了ID,则这个对象就当作游离态处理:
当ID在数据库中不能找到时,用update的话肯定会报异常,然而用merge的话,就会insert。
当ID在数据库中能找到的时候,update与merge的执行效果都是更新数据,发出update语句;
如果没有设置ID的话,则这个对象就当作瞬态处理:
用update的话,由于没有ID,所以会报异常,merge此时则会保存数据,根据ID生产策略生成一条数据;
问题解析:http://lcy0202.iteye.com/blog/1453868
阅读全文
0 0
- Hibernate Exception: a different object with the same identifier value was already associated with the session
- Hibernate更新a different object with the same identifier value was already associated with the session
- [Hibernate]a different object with the same identifier value was already associated with the session
- Hibernate:a different object with the same identifier value was already associated with the session
- hibernate a different object with the same identifier value was already associated with the session
- Hibernate :A different object with the same identifier value was already associated with the session
- hibernate:a different object with the same identifier value was already associated with the session
- 解决a different object with the same identifier value was already associated with the session错误
- 解决a different object with the same identifier value was already associated with the session错误
- a different object with the same identifier value was already associated with the session的解决办法
- 解决a different object with the same identifier value was already associated with the session错误
- a different object with the same identifier value was already associated with the session
- a different object with the same identifier value was already associated with the session
- 解决a different object with the same identifier value was already associated with the session错误
- a different object with the same identifier value was already associated with the session
- a different object with the same identifier value was already associated with the session
- 再次碰到:a different object with the same identifier value was already associated with the session
- a different object with the same identifier value was already associated with the session
- poj3041 Asteroids 最小点覆盖 二分图 匈牙利算法BFS
- 代码行数统计
- Python查询mssql导入Excel表格
- 【Java多线程】Java内存模型与Volatile
- MyEclipse javaScript .时出现卡死现象
- hibernate a different object with the same identifier value was already associated with the session
- PageHelper插件
- MySQL修改root密码的多种方法
- 2017 Multi-University Training Contest 9 solutions 1008 Numbers(hdu6168)
- ggplot2的基本绘图知识
- Java 开发环境配置-安装jdk1.8
- window下安装RBTools和使用
- Andriod 旋转动画RotateAnimation设置无限循环播放
- oracle学习历程 2017/8/29