org.hibernate.NonUniqueObjectException的原因与解决方法
来源:互联网 发布:mac登录界面英文 编辑:程序博客网 时间:2024/05/18 00:42
org.hibernate.NonUniqueObjectException的原因与解决方法
使用hibernate更新对象时,出现如下错误:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:[com.fatkun.dao.hibernate.User#12]
原因
在同一个session内,如果已经有一个对象已经是持久化状态(load进来等),现在构造一个新的PO,和前一个持久化对象拥有相同的持久化标识(identifier),在update的时候,就会抛这个错误。
举个例子(伪代码):
User user1 = session.load(1);User user2 = new User();user2.setId(1);//此时ID和user1一样user2.setUsername("fatkun");session.update(user2);//这里会抛出错误
解决方法
1.不要重新new一个对象,使用load的对象对他进行更改值。
例如上面例子直接对user1操作,最后更新user1
2.如果是hibernate3以上,可以使用session.merge()方法
3.把session中同标识的对象移出(session.evict(user1)),使他成为脱管的状态,然后user2就可以update了。
4.使用session.clear()清除session中的缓存数据(不管缓存与数据库的同步)
0 0
- org.hibernate.NonUniqueObjectException的原因与解决方法
- org.hibernate.NonUniqueObjectException的原因与解决方法
- org.hibernate.NonUniqueObjectException解决方法
- 出现 org.hibernate.NonUniqueObjectException 错误的解决方法
- org.hibernate.NonUniqueObjectException异常解决方法
- org.hibernate.NonUniqueObjectException异常解决方法
- 关于org.hibernate.NonUniqueObjectException出现的一种情况,以及解决方法。
- org.hibernate.NonUniqueObjectException异常解决方法(1)
- org.hibernate.NonUniqueObjectException异常解决方法(2)
- org.hibernate.NonUniqueObjectException两种解决方法
- org.hibernate.NonUniqueObjectException两种解决方法
- org.hibernate.NonUniqueObjectException:org.hibernate.NonUniqueObjectException: a different obj异常解决方法
- org.hibernate.NonUniqueObjectException 个人的解决办法
- org.hibernate.NonUniqueObjectException的错误,很蛋疼
- org.hibernate.NonUniqueObjectException 个人的解决
- org.hibernate.NonUniqueObjectException
- org.hibernate.NonUniqueObjectException
- org.hibernate.NonUniqueObjectException
- LeetCode之longst Consecutive Sequence
- SDUT 2867 小学奥数(下)(规律)
- Android 自定义title 之Action Bar
- 黑马程序员:3.面向对象(上)
- C++下遍历文件夹
- org.hibernate.NonUniqueObjectException的原因与解决方法
- 汇编指令速查
- linux2.6.32系统移植过程总结
- LCS 第一次!
- 安卓学习之控件布局LinearLayout示例
- cocos2d第一天总结
- 记录Ubunut下搭建JAVA环境
- [dp] zoj 3872 Beauty of Array
- 时间序列基础包(zoo,zooreg)