Hibernate的一个经典异常(org.hibernate.NonUniqueObjectException)

来源:互联网 发布:mac cad2014破解 编辑:程序博客网 时间:2024/05/21 14:56

异常为:

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 

意思是拥有相同的数据库标识符(代表数据库中的同一条记录),但是具有不同的持久化标识符。


解决办法

1 做一个动作将session关闭 调用Session的 close方法 ,Session的缓存被清空,其中的所有持久化对象都变为游离对象, 
2 调用Session的evict()方法能够从缓存中删除一个持久化对象,使其变为游离状态,这样也能达到解决同一个 
session中关联两个同一个identifer的对象更新的时候产生的冲突 

3 使用session.merge()方法保存对象。

要理解一下使用hibernate时对象的三种状态。

看http://blog.csdn.net/hongchangfirst/article/details/8111824

0 0
原创粉丝点击