转自: 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. TVS(瞬间电压抑制器)

    1.原理 TVS二极管在线路板上与被保护线路并联,当瞬时电压超过电路正常工作电压后,TVS二极管便产生雪崩,提供给瞬时电流一个超低电阻通路,其结果是瞬时电流透过二极管被引开,避开被保护元件,并且在电压 ...

  2. 2.虚拟机安装的ubuntu全屏显示

    虚拟机下面安装了ubuntu系统,显示的屏幕只有那么一小块儿,不知道如何才能全屏,那么如何全屏呢?且看下面经验. 方法/步骤 打开虚拟机,并点击要更改成全屏的那个ubuntu系统的电源 我的虚拟机名字 ...

  3. 4.css基础

    1 Css概念 CSS 指层叠样式表 (Cascading Style Sheets)(级联样式表) Css是用来美化html标签的,相当于页面化妆. ◆样式表书写位置 2选择器 2.1 写法 选择器 ...

  4. AlexNet详解3

    Reference. Krizhevsky A, Sutskever I, Hinton G E. ImageNet Classification with Deep Convolutional Ne ...

  5. neo4j图数据库安装以及基本操作命令

    neo4j图数据安装以及基本操作命令 neo4j安装配置使用, 安装环境:Ubuntu系统 jdk1.8 (neo4j 只支持jdk1.8以上版本) 下载 neo4j neo4j的下载window可以 ...

  6. dev gridview自动列宽和单元、行、checkbox选中和多选

    #region 自动列宽 for (int I = 0; I < gridView1.Columns.Count; I++) { this.gridView1.BestFitColumns(); ...

  7. win10 开机背景图

    地址 C:\Users\*\AppData\Local\Packages\Microsoft.Windows.ContentDeliveryManager_cw5n1h2txyewy\LocalSta ...

  8. C# 动态创建SQL数据库(二)

    使用Entity Framework  创建数据库与表 前面文章有说到使用SQL语句动态创建数据库与数据表,这次直接使用Entriy Framwork 的ORM对象关系映射来创建数据库与表 一 新建项 ...

  9. EF t4模板将实体与DBContext分离

    在用EF DBFirst时,实体类是从数据库自动生成的,与DBContext放在同一个项目中.这样其他项目想引用实体,就会将数据库操作类暴露出来.所以,我们需要将实体分离. 新建项目EFAccess, ...

  10. nyoj 1274信道安全 第九届河南省赛(SPFA)

    信道安全 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 Alpha 机构有自己的一套网络系统进行信息传送.情报员 A 位于节点 1,他准备将一份情报 发送给位于节点 ...