转自: 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. robotium测试创建java文件和junit文件区别

    两者本身差别不大,只是构造方式不同,我一般都用java文件来创建. 1.当有源码测试时,创建junit测试文件 ActivityInstrumentationTestCase2<T>T可以 ...

  2. IT项目管理流程以及每个步骤用到的文档

    IT项目管理从大的方面可分为:1)项目启动阶段:2)项目计划阶段:3)项目的实施阶段:4)项目的结项阶段 1)项目启动阶段: 1.项目启动流程规范: 1.1项目启动的简介.目的和范围 1.2目的可行性 ...

  3. My first paper is now available online

    A two-grid discretization scheme of non-conforming finite elements for transmission eigenvalues

  4. Python自动化开发 - 字符编码、文件和集合

    本节内容 字符编码 文件操作 集合 一.字符编码 1.编码 计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.解决思路:数字与符号建立一对一映射,用不同数字表示不同符号. ASCI ...

  5. Mac系统登录不进系统解决办法

    1.找到买苹果电脑时附带的 Mac OS X 系统光盘,或者有苹果 Mac OS X 系统镜像的 U 盘/移动硬盘,塞入光驱(或插在 USB /火线接口上).重启苹果电脑,开机时按住“option”键 ...

  6. 【图数据结构的遍历】java实现广度优先和深度优先遍历

    [图数据结构的遍历]java实现广度优先和深度优先遍历 宽度优先搜索(BFS)遍历图需要使用队列queue数据结构: 深度优先搜索(DFS, Depth First Search)的实现 需要使用到栈 ...

  7. C# 用户选择单个压缩-系统自带压缩

    //用C#自带的压缩,最少要.net4.5或以上,先增加引用 System.IO.Compression.FileSystem // FolderBrowserDialog dlg = new Fol ...

  8. 5. ASP.NET MVC 中的Areas【区域】是什么

    [PS返回上一篇:-->4.ASP.NET MVC 5.0 视图之模型绑定] 从ASP.NET MVC 2.0开始,微软就提供了一个新特性:Areas[区域].Areas仅仅是用来将大型程序拆分 ...

  9. .net图表之ECharts随笔04-散点图

    见图说话,修改参数option实现上图显示: 1. 共用参数title还有一个属性subtext,可以用来设置副标题 2. tooltip与toolbox也是共用属性 3. dataZoom是设置滚动 ...

  10. [leetcode.com]算法题目 - Pascal's Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...