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

原因:在hibernate中同一个session里面有了两个相同标识但是是不同实体。通常来说,使用session的merge(object)便可以解决。

但也有其他令几种方式供参考:

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)

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

Hibernate Error: a different object with the same identifier value was already associated with the session的更多相关文章

  1. 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 ...

  2. 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 ...

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

  4. hibernate 异常a different object with the same identifier value was already associated with the session

    在使用hibernate的时候发现了一个问题,记录一下解决方案. 前提开启了事务和事务间并无commit,进行两次save,第二次的时候爆出下面的异常a different object with t ...

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

    原先跑TEST CASE的时候没有出错 但是跑到整个程序里面,除了这个问题, 网上也找了下资料,说是用merge之类的可以解决,因为你这个update的obj和session里面的不用,所以导致此问题 ...

  6. org.hibernate.NonUniqueObjectException: a different object with the same identifier value was alread---------程序报错

    今天遇到了这个问题: org.hibernate.NonUniqueObjectException: a different object with the same identifier value ...

  7. 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"?> ...

  8. Hibernate更新数据报错:a different object with the same identifier value was already associated with the session: [com.elec.domain.ElecCommonMsg#297e35035c28c368015c28c3e6780001]

    使用hibernate更新数据时,报错 Struts has detected an unhandled exception: Messages: a different object with th ...

  9. 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 ...

随机推荐

  1. eclipse报错资料备份

    一. eclipse中初始化控件出错 在添加 TextView myTextView=(TextView)this.findViewById(R.id.myTextView); Button myBu ...

  2. yum错误,Cannot find a valid baseurl for repo: base 和 No more mirrors to try

    可能出错原因: 1. yum 配置错误 2. 虚拟机无法连接外网 3. 域名解析没有 如何解决这个错误? 1. 网上找 /ect/yum.conf 和 /etc/yum.repos.d/CentOS- ...

  3. js_10_dom表单

    事件的优先级? 先执行事件,后执行标签内置事件,如果事件返回false不执行后面的事件或标签内置事件 如何通过js提交表单? 任意标签定义onclick事件 函数中写入:document.getEle ...

  4. 重温吕鑫MFC教学视频(一)

    重温吕鑫MFC教学视频(一)1. picture控件的使用,可以显示icon和bitmap2. WM_Create窗口的创建3. 创建的销毁消息及区别WM_SYSCOMMAND WM_CLOSE WM ...

  5. 输入和输出--javase中的路径

    就目前为止, javase中经常用到路径来读取一个资源文件的所有情况都已经整理在博客里面了,这里做一个统一的整理: 1,IO流来读取一个文件,比如说new FileInputStream(" ...

  6. The mkdir Command

    The mkdir command is is used to create new directories. A directory, referred to as a folder in some ...

  7. java 如何将 word,excel,ppt如何转pdf --openoffice (1)

    承上启下,可折叠 上一篇说的是:服务器是windows server时,用jacob将msoffice(指的是word,excel,ppt)转换成pdf. 若被部署项目的服务器是centOS等linu ...

  8. VS中代码覆盖问题

    在VS中编写代码时,需要插入代码是,经常是将插入点后面的代码覆盖掉而不是将它向后推. 解决这样的问题,只需要按   Insert 键即可, 我的笔记本是   Fn 加 del

  9. 小白学Docker之Compose

    承接上篇文章:小白学Docker之基础篇,自学网站来源于https://docs.docker.com/get-started 概念 Compose是一个编排和运行多容器Docker应用的工具,主要是 ...

  10. iOS-键盘监听YYKeyboardManager

    如果键盘弹出覆盖了原有的试图,这种效果并不好,所以我们就要在键盘弹出的时候,监听键盘的位置来改变我们一些试图的位置,例如tableView列表等:在这里推荐一个大牛ibireme写的YYKeyboar ...