问题场景:

  (我要做对象修改,然后保存进数据库)

  我通过数据库获取一个原对象obj;

  然后新增了一个修改对象obj1;

  然后将obj1.setId(obj.getId());

  然后调用数据库实例对象SESSION.UPDATE(OBJ1);

数据库报错,抛出了异常a different object with the same identifier value was already associated with the session。

  原因就是session再检测实例化对象时,发现obj和obj1的ID相同(通过地址去检测),导致抛出异常,根据以下解决方法:

  buildDevelopers_tDAO.getSessionFactory().getCurrentSession().merge(buildDev_t_after);

  即 使用MERGE()方法,而不是使用update()方法;

  

  另外一种解决方法:

  不要使用set()方法去赋ID;

  obj = obj1;

  obj.setName()

  .....(根据相应的修改,去进行赋值)

  然后调用数据库实例对象SESSION.UPDATE(OBJ1);

  即不要使用set方法赋ID即可。

解决方案:

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)

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

3、问题情况:使用hibernate来进行对对象的保存操作时,出现了exception,导致数据保存不成功,具体报错如是:

Exception in thread "main" org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:

使用情况(本人遇到):如果是在保存对象的时候,如果对象有外键,则在保存的时候,要根据外键来获取数据库所对应的记录的对象,之后再set入要保存的的对象中,如果是直接new一个外键的对象(外键的值在数据库有对应的值)再set入保存的对象中的时候,就会报错:Exception in thread "main" org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:,这个是因为,在保存的时候,session会检查在内存的外简单的对象跟你new的对象是否一样(不是根据对象的值,而是根据在内存的地址是否一致来比较的),所以new出来的对象是无法跟内存里面的对象一致的,因而,会说不同的对象有一样的id,所以保存数据库的时候就直接根据外键来获取对应的数据库记录来保存对象,所以这个时候用merge方法还是解决不了问题的。

最后是在查了一下,采用session.merge (object c)代替session.save(object c),即可解决,主要涉及到session.merge ()方法的使用以及session.merge ()跟session.save()、session.update ()方法的区别,这篇博文详细说明了问题....

该方法将修改表中记录,其所需要的实体状态为脱管状态,但是注意,它并不影响调用方法前后的状态,也即该实体依然是脱管状,见例6.4。

例6.4:session.merge ()方法对状态的变化

public void run() {

//创建UserInfo实例

UserInfo userInfo = new UserInfo();

//使之成为脱管状态

userInfo.setId(11112);

userInfo.setName("RW3");

userInfo.setSex("M");

//创建UserInfo实例

UserInfo userInfo2 = new UserInfo();

//使之成为脱管状态

userInfo2.setId(11112);

userInfo2.setName("RW4");

userInfo2.setSex("F");

//启动Session

Session session = HibernateSessionFactory.currentSession();

//启动事务

Transaction tx = session.beginTransaction();

//调用merge方法,此时UserInfo实体状态并没有被持久化

session.merge(userInfo);

//调用merge方法,此时UserInfo实体状态并没有被持久化

//但是数据库中的记录被更新了

①session.merge(userInfo2);

//merge方法与update方法的差别在于针对同样的操作update方法会报错

//原因在于update方法使得实体状态成为了持久化状态,而Session中不允许两个持久化实体有同样的持久化标识

②//session.update(userInfo);

//session.update(userInfo2);

//以下两句不会发送SQL,因为userInfo2不是持久化状态的实体

③userInfo2.setName("RW5");

userInfo2.setSex("M");

//提交事务

tx.commit();

//关闭Hibernate Session

HibernateSessionFactory.closeSession();

}

针对该段代码将执行如下SQL语句:

Hibernate:

/* ①session.merge(userInfo2)的动作 */

select

userinfo0_.id as id0_0_,

userinfo0_.NAME as NAME0_0_,

userinfo0_.SEX as SEX0_0_,

userinfo0_.roomid as roomid0_0_

from

userinfo userinfo0_

where

userinfo0_.id=?

Hibernate:

/* ①session.merge(userInfo2)的动作 */

update

userinfo

set

NAME=?,

SEX=?,

roomid=?

where

id=?

session.merge()方法会首先发送一句select语句,去数据库端获取UserInfo持久化标识所对应的表记录;然后自动生成一个持久化状态的UserInfo实体,与脱管状态的UserInfo实体做比较是否有所改变;一旦发生了改变,才会发送update语句执行更新。而按执行顺序,若两句session.merge()方法针对同一个脱管状态的UserInfo实体,那其结果只会执行最后一个session.merge()方法所发出的update语句。即使执行了session.merge()方法,UserInfo实体依然是脱管状态,因此③userInfo2. setName("RW5")的语句不会同步数据库中的表。

转载于:https://www.cnblogs.com/UUUz/p/9930292.html

解决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. 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时,一般是因为在 ...

  3. 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 associ ...

  4. 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解决方案 参考文章: ...

  5. 解决The given object has a null identifier问题

    首先阐述下问题.第一次做的是插入操作,第二次在来是进行更改.用的是hibernate框架操作.如果遇到和我相同的问题,可能可以借鉴一下,我是这么解决的! 首先在实体类重写tostring方法. act ...

  6. a different object with the same identifier val...

    2019独角兽企业重金招聘Python工程师标准>>> a different object with the same identifier value was already a ...

  7. org.hibernate.TransientObjectException:The given object has a null identifier

    1.错误描述 org.hibernate.TransientObjectException:The given object has a null identifier:com.you.model.U ...

  8. 关于hibernate 更新或者删除报错different object with the same identifier

    Hibernate 疑难异常及处理 1.a different object with the same identifier value was already associated with th ...

  9. 关于出现org.hibernate.TransientObjectException: The given object has a null identifier: 错误的解决方法

    关于出现org.hibernate.TransientObjectException: The given object has a null identifier: 错误的解决方法 参考文章: (1 ...

最新文章

  1. 潜移默化学会WPF--绘图 学习(一)
  2. 51nod挑的部分5级题
  3. Oracle优化11-10046事件
  4. Web前端性能优化——编写高效的JavaScript
  5. 数组之冒泡排序、选择排序
  6. Window10:不能建立到远程计算机的连接。你可能需要更改此连接的网络设置。
  7. 关于DDD中Domain的思考
  8. es6 Decorator类的修饰器
  9. python 简单检索器_python实现文件搜索工具(简易版)
  10. C++ explicit关键字详解(用于构造函数)
  11. php将中文编译成字符串,PHP将汉字字符串转换为数组
  12. python lambda表达式及用法_Python中lambda表达式的常见用法
  13. 认识并学会springCloud的使用
  14. debug, release strlen与sizeof
  15. 计算机视觉 CS231n Course Introduction
  16. php挑战答题,挑战答题小程序5.3.1开源
  17. 高性能至强融核服务器,剖析两大至强融核产品规格_Intel服务器CPU_服务器评测与技术-中关村在线...
  18. Concatenated Multiples(串联倍数)
  19. mysql存储爬虫图片_世纪佳缘信息爬取存储到mysql,下载图片到本地,从数据库选取账号对其发送消息更新发信状态...
  20. NB,用这一篇文章带你了解什么是爬虫?

热门文章

  1. 【Pytorch神经网络实战案例】15 WGAN-gp模型生成Fashon-MNST模拟数据
  2. linux装机量,在没有盗版的世界 Linux桌面的装机量可能占比达到40%
  3. mysql lib 5.5.28_mysql5.5.28在Linux下的安装
  4. mysql数据库用户的创建_mysql创建用户及数据库
  5. Vue权限控制——动态注册路由
  6. LeetCode 2057. 值相等的最小索引
  7. LeetCode 1816. 截断句子
  8. LeetCode 1629. 按键持续时间最长的键
  9. sklearn 机器学习 Pipeline 模板
  10. LeetCode 1296. 划分数组为连续数字的集合(map模拟)