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 ...
随机推荐
- lufylegend游戏引擎
lufylegend游戏引擎介绍:click 这个链接我觉得已经很详细的介绍了这个引擎. 所以以下我只说说一些简单的游戏代码过程. 首先从canvas做游戏叙述起: 这是一个让人很熟悉的简单小游戏,网 ...
- SQL Server的小数数值类型(float 和 decimal)用法
在SQL Server中,小数数值实际上只有两种数据类型:float 和 decimal.double precision 整体是数据类型,等价于 float(53),real等价于float(24) ...
- LLBL Gen Template Studio 2.x
Template Studio for LLBLGen Pro released Today we released Template Studio, a full-featured IDE for ...
- 深入理解PHP内核(二)概览-PHP生命周期与Zend引擎
本文参考自<深入理解PHP内核>,地址:https://github.com/reeze/tipi 本文链接:http://www.orlion.ml/232/ 1.SAPI接口 SAPI ...
- javascript类型系统——Number数字类型
× 目录 [1]定义 [2]整数 [3]浮点数[4]科学记数[5]数值精度[6]数值范围[7]特殊数值[8]转成数值[9]实例方法 前面的话 javascript只有一个数字类型,它在内部被表示为64 ...
- java 模拟qq源码
java 模拟qq源码: http://files.cnblogs.com/files/hujunzheng/QQ--hjzgg.zip
- Android TextView高级特性使用
TextView一般都是用来显示一段文本,这里说的高级特性主要是一些我们平常不太常用的属性.包括文字阴影.自定义字体.html嵌入多格式.字体加粗.插入图片.这些特性平时开发APP的时候,可能一般使用 ...
- 一个在ASP.NET中利用服务器控件GridView实现数据增删改查的例子
备注:这是我辅导的一个项目开发组的一个例子,用文章的方式分享出来,给更多的朋友参考.其实我们这几年的项目中,都不怎么使用服务器控件的形式了,而是更多的采用MVC这种开发模式.但是,如果项目的历史背景是 ...
- 拓扑排序(三)之 Java详解
前面分别介绍了拓扑排序的C和C++实现,本文通过Java实现拓扑排序. 目录 1. 拓扑排序介绍 2. 拓扑排序的算法图解 3. 拓扑排序的代码说明 4. 拓扑排序的完整源码和测试程序 转载请注明出处 ...
- Android基于mAppWidget实现手绘地图(六)–如何展示地图对象
为了展示选中的点,你需要完成以下步骤: 1.创建或者获得一个已经存在的图层 2.创建代表选中点的地图对象 3.把地图对象添加到图层 创建新图层 使用以下代码片段创建图层 int COFFEE_SHOP ...