转自: http://blog.csdn.net/zzzz3621/article/details/9776539

看异常提示意思已经很明显了,是说主键不唯一,在事务的最后执行SQL时,session缓存里面有多个(>1)主键一样的对象。

了解过hibernate的都知道它有一个一级缓存,即session级别的缓存,在一个事务的执行过程中可以管理持久化对象,在事务最后执行SQL,可以减少数据库的操作。

报这个异常就得仔细看看你的代码,一定有地方引用的对象已经不同了。

下面就是一个典型的例子:

  1. public void update(Object obj){
  2. fillObject(obj);
  3. session.update(obj);
  4. }
  5. public void fillObject(Object obj){
  6. Object obj2 = session.load(obj.getId());
  7. //DO 设置obj2的属性
  8. //错误的地方
  9. obj = obj2;
  10. }

正确的应该是

  1. public void update(Object obj){
  2. obj = fillObject(obj);
  3. session.update(obj);
  4. }
  5. public Object fillObject(Object obj){
  6. Object obj2 = session.load(obj.getId());
  7. //DO 设置obj2的属性
  8. return obj2;
  9. }

错误的情况:在刚调用fillObject方法的时候obj(fillObject)是1的状态,后面变成了2。但是obj(update)一直是指向obj的,当执行完fillObject后,session已经存放了obj2,再将obj更新到数据库的时候就会出错。

org.hibernate.NonUniqueObjectException:a different object with the same identifier value was alread的更多相关文章

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

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

  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:

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

  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. 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.TransientObjectException:The given object has a null identifier

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

  7. 报错HTTP Status 500 - The given object has a null identifier: cn.itcast.entity.Customer; nested exception is org.hibernate.TransientObjectException: The given object has a null identifier:

    在使用模型驱动封装的时候,要保证表单中name属性名和类中属性名一致,否则将会报错如下: HTTP Status 500 - The given object has a null identifie ...

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

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

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

随机推荐

  1. android 线程中断的处理

    android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@4 ...

  2. 《mysql必知必会》学习_第10章_20180731_欢

    第10章,计算字段. P64 select concat (vend_name,'(',vend_country,')') from vendors order by vend_name; # 拼接, ...

  3. 9.DataGrid数据表格

    后台获取数据并将其转换为json数组格式: 前台获取数据并显示在数据表格中:

  4. SRM483

    250pt 题意:给定一个[0,1)间的实数,一个分母不超过maxDen的分数逼近.. 思路:直接枚举.然后判断. code: #line 7 "BestApproximationDiv1. ...

  5. CSS 基础 例子 盒子模型及外边距塌陷

    我们通常设置的宽度和高度,是指盒子模型中内容(content)的宽度和高度.元素的高度,还要加上上下padding和上下border,元素整个盒子的高度还要加上上下margin:宽度类似计算. 注意: ...

  6. 木马入侵查杀 linux

     目 录: 一.问题现象: 二.问题排查: 1.netstat 排查: 2.top查看: 3.lsof -c 命令排查: 4.确定中木马了. 三.木马查杀: 木马1,清除: 木马2,清除: 四.后续处 ...

  7. HyperServer 中的 SSL 支持

    HyperServer 中的 SSL 支持 DLL 模式不需要 SSL 配置, 因为 web 服务器 (如 IIS) 将承担 ssl 配置和 ssl 证书的责任. 对于独立和服务模式, ssl 配置是 ...

  8. 一个DELPHI操作USB摄像头类

    最近在使用Usb摄像头做了个项目,其中写了一个操作usb摄像头类分享给大家 {*******************************************************} { } ...

  9. cxGrid实现取消过滤和排序后定位到首行(单选和多选)

    cxGrid实现取消过滤和排序后定位到首行(单选和多选) 原创 2013年10月06日 18:42:24 2107 DataContoller中的函数FocusedRecordIndex没有反应,Fo ...

  10. Azure DevOps Server: 使用Rest Api获取拉取请求Pull Request中的变更文件清单

    需求: Azure DevOps Server 的拉取请求模块,为开发团队提供了强大而且灵活的代码评审功能.拉取请求中变更文件清单,对质量管理人员,是一个宝贵的材料.质量保障人员可以从代码清单中分析不 ...