org.hibernate.NonUniqueObjectException: a different object with the same identifier value was alread---------程序报错
今天遇到了这个问题:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:……
找其原因是因为同一个object,如一个person在seession里保存了一份,而增加的别的object,如company的时候,由于做了关联关系,从数据库里又get了一个person,而这个person和seession里的person是同一个,把get出来的person set 到了company里,像这样,compnay.setPerson(person).在增加company的时候就出现了这个错误。
网上查了一下,在增加object时,先做一个这样的操作,说是把相同的object合并一下:
object = session.merge(object);
然后再保存
session.save(object);
我试了试,确实可以解决。
但因为我的操作中,在增加之后还有一个修改的操作,增加时是成功了,但在修改的时候就又出新的错了,所以我最后的解决办法是:
用程序来判断,在拿person的时候,session里有就从session里拿,没有再get出来,最后再保存company,就没问题了。
也可以在action 中判断 sessiion中是不是已经存在此对象,如果存在,就不要去数据库中再拿了,或者拿出来之后判断此对象和session中是否一致,如果一致就用Session 中的就可以了
转载于:http://blog.csdn.net/xinmashang/article/category/1120644
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was alread---------程序报错的更多相关文章
- org.hibernate.NonUniqueObjectException:a different object with the same identifier value was alread
转自: http://blog.csdn.net/zzzz3621/article/details/9776539 看异常提示意思已经很明显了,是说主键不唯一,在事务的最后执行SQL时,session ...
- 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 ...
- 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 ...
- 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 ...
- rg.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:
原先跑TEST CASE的时候没有出错 但是跑到整个程序里面,除了这个问题, 网上也找了下资料,说是用merge之类的可以解决,因为你这个update的obj和session里面的不用,所以导致此问题 ...
- 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 ...
- 报错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 ...
- 运行编译后的程序报错 error while loading shared libraries: lib*.so: cannot open shared object file: No such file or directory
运行编译后的程序报错 error while loading shared libraries: lib*.so: cannot open shared object file: No such f ...
- 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 ...
随机推荐
- Web应用安全之文件上传漏洞详解
什么是文件上传漏洞 文件上传漏洞是在用户上传了一个可执行的脚本文件,本通过此脚本文件获得了执行服务器端命令的功能,这种攻击方式是最为直接,最为有效的,有时候,几乎没有什么门槛,也就是任何人都可以进行这 ...
- 【WP开发】手电筒
或许很多人都想到,可以利用手机上摄像头的闪光灯做手电筒,当然,有利必有害,每次使用的时间不要过长,几分钟一般不会有什么问题,如果时间太长,难保会有损伤. 以往的方案是调用视频录制功能来开始录制视频,同 ...
- 深入理解PHP内核(五)函数的内部结构
php的函数包括用户定义的函数.内部函数(print_r count...).匿名函数.变量函数($func = 'print_r'; $func(array('a','b'));) PHP内核源码中 ...
- 深入理解javascript作用域系列第一篇——内部原理
× 目录 [1]编译 [2]执行 [3]查询[4]嵌套[5]异常[6]原理 前面的话 javascript拥有一套设计良好的规则来存储变量,并且之后可以方便地找到这些变量,这套规则被称为作用域.作用域 ...
- CSS中常见的6种文本样式
前面的话 CSS文本样式是相对于内容进行的样式修饰.由于在层叠关系中,内容要高于背景.所以文本样式相对而言更加重要.有些人对文本和字体样式之间的不同不太了解,简单地讲,文本是内容,而字体则用于显示这个 ...
- php操作mongodb中的ISODate格式日期
mongodb 中数据记录的日期格式为"dateCreated" : ISODate("2011-12-20T07:22:50.836Z")经过翻阅php官网中 ...
- EasyUI treegrid 获取编辑状态中某字段的值 [getEditor方法获取不到editor]
如题,在treegrid里,按照api里getEditor方法的说明, getEditoroptionsGet the specified editor, the options contains t ...
- sqlserver 存储过程中使用临时表到底会不会导致重编译
曾经在网络上看到过一种说法,SqlServer的存储过程中使用临时表,会导致重编译,以至于执行计划无法重用, 运行时候会导致重编译的这么一个说法,自己私底下去做测试的时候,根据profile的跟踪结果 ...
- Android SlidingMenu 仿网易新闻客户端布局
前面两篇文章中的SlidingMenu都出现在左侧,今天来模仿一下网易新闻客户端左右两边都有SlidingMenu的效果,以下是网易新闻客户端效果: 不扯闲话了,直接进入正题吧 frame_conte ...
- javascript中遇到的字符串对象处理
在javascript中对参数处理: 1 <script> 2 function getParam() 3 { 4 urlInfo=window.location.href; //获取当前 ...