[置顶] Hibernate的一个经典异常

异常为:

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


原文:http://blog.csdn.net/hongchangfirst/article/details/8506685

作者:hongchangfirst

hongchangfirst的主页:http://blog.csdn.net/hongchangfirst



原文地址:https://www.cnblogs.com/keanuyaoo/p/3278028.html