1、a different object with the same identifier value was already associated with the session。

  错误原因:在hibernate中同一个session里面有了两个相同标识但是是不同实体。

  解决方法一:session.clean()

  PS:如果在clean操作后面又进行了saveOrUpdate(object)等改变数据状态的操作,有可能会报出"Found two representations of same collection"异常。

  解决方法二:session.refresh(object)

  PS:当object不是数据库中已有数据的对象的时候,不能使用session.refresh(object)因为该方法是从hibernate的session中去重新取object,如果session中没有这个对象,则会报错所以当你使用saveOrUpdate(object)之前还需要判断一下。

  解决方法三:session.merge(object)

  PS:Hibernate里面自带的方法,推荐使用。

2、Found two representations of same collection

  错误原因:见1。

  解决方法:session.merge(object)

以上两中异常经常出现在一对多映射和多对多映射中

举一下自己的代码

    /**
* 取得当前Session.
* @return Session
*/
public Session getSession() {
return sessionFactory.getCurrentSession();
} /**
* 保存新增或修改的对象.
* @param entity
*/
public void save(final T entity) {
getSession().saveOrUpdate(getSession().merge(entity));
}

[转]解决a different object with the same identifier value was already associated with the session错误的更多相关文章

  1. 解决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错误 这个错 ...

  2. hibernate中一种导致a different object with the same identifier value was already associated with the session错误方式及解决方法

    先将自己出现错误的全部代码都贴出来: hibernate.cfg.xml <?xml version="1.0" encoding="UTF-8"?> ...

  3. 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 thesession时,一般是因为在h ...

  4. org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session异常解决办法

    org.hibernate.NonUniqueObjectException: a different object with the same identifier value was alread ...

  5. a different object with the same identifier value was already associated with the session:

    hibernate操作: 实例化两个model类,更新时会提示  a different object with the same identifier value was already assoc ...

  6. Hibernate Error: a different object with the same identifier value was already associated with the session

    在执行Hibernate的Update操作时,报错:a different object with the same identifier value was already associated w ...

  7. a different object with the same identifier value was already associated with the session解决方案

    org.springframework.orm.hibernate3.HibernateSystemException: a different ]; nested exception ] at or ...

  8. Exception 06 : org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session :

    异常名称: org.hibernate.NonUniqueObjectException: A different object with the same identifier value was ...

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

    保存实体异常 https://blog.csdn.net/zzzz3621/article/details/9776539 org.hibernate.NonUniqueObjectException ...

随机推荐

  1. oracle更新语句merge和update

    update: update语句更新需要根据索引或者数据列遍历所有行 语句举例: update table1  a set column1=(select column from table2 b w ...

  2. eclipse连接mysql,插入数据时乱码

    问题:如果eclipse中项目的编码方式为utf-8 插入数据后,在数据库中查看后,汉字出现乱码情况 解决方法: 1.在获取连接的时候将conn = DriverManager.getConnecti ...

  3. LeetCode() Symmetric Tree

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  4. 【OpenGL】第二篇 Hello OpenGL

    ---------------------------------------------------------------------------------------------------- ...

  5. php中奖概率算法,可用于刮刮卡,大转盘等抽奖算法

    php中奖概率算法,可用于刮刮卡,大转盘等抽奖算法.用法很简单,代码里有详细注释说明,一看就懂 <?php /* * 经典的概率算法, * $proArr是一个预先设置的数组, * 假设数组为: ...

  6. php区分new static 和new self

    关键点在于一个是静态绑定,一个是延迟绑定 <?php class A{ public function __construct() { } public function createObjSt ...

  7. cps变换

    网上看了很多内容,很少有给出一个准确的概念,它的英文全称是continuous passing style, 直译为连续传递样式,那么cps transform就是将一些原本不是continuous ...

  8. 总结.NET 中什么时候用 Static

    静态类和类成员用于创建无需创建类的实例就能够访问的数据和函数.当类中没有依赖对象标识的数据或行为时,就可以使用静态类.静态类成员是可独立于任何对象标识的数据和行为,即无论对象发生什么更改,这些数据和函 ...

  9. 转载:python中的StringIO模块

    注意:python3中应使用io.StringIO StringIO经常被用来作为字符串的缓存,应为StringIO有个好处,他的有些接口和文件操作是一致的,也就是说用同样的代码,可以同时当成文件操作 ...

  10. 实战之中兴ZXHN F460光猫破解超级密码+开启无线路由功能

    本文面向小白用户,即使你不懂电脑看完你也会破解光猫,网上有些文章的操作方法是错误的.按照我这篇文章,只要型号对,那么肯定没问题!电信光纤入户,家里用的是电信送的中兴查看 ZXHN F460 中的全部文 ...