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 ...
随机推荐
- Python数据类型之“文本序列(Text Sequence)”
Python中的文本序列类型 Python中的文本数据由str对象或字符串进行处理. 1.字符串 字符串是Unicode码值的不可变序列.字符串字面量有多种形式: 单引号:'允许嵌入"双&q ...
- 接触LLBL Gen Pro 对象关系映射框架后 前途变的一片光明
时间回到2010年,那时候还是熟悉代码生成+基础框架这种模式,基本的开发思路是通过代码生成器生成实体,再生成接口与实现类,最后拖拉控件,写界面数据绑定代码.基本上就是动软代码生成器给出的模式,或是微软 ...
- CSS两列高度自适应,右边自适应
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- CSS伪类与CSS伪元素的区别及由来
关于两者的区别,其实是很古老的问题.但是时至今日,由于各种网络误传以及一些不负责任的书籍误笔,仍然有相当多的人将伪类与伪元素混为一谈,甚至不乏很多CSS老手.早些年刚入行的时候,我自己也被深深误导,因 ...
- SOLID原则
SOLID是面向对象设计和编程(OOD&OOP)中几个重要编码原则 即:SRP单一责任原则: OCP开放封闭原则: LSP里氏替换原则: ISP接口分离原则: DIP依赖倒置原则. 1. 单一 ...
- 哈夫曼树(三)之 Java详解
前面分别通过C和C++实现了哈夫曼树,本章给出哈夫曼树的java版本. 目录 1. 哈夫曼树的介绍 2. 哈夫曼树的图文解析 3. 哈夫曼树的基本操作 4. 哈夫曼树的完整源码 转载请注明出处:htt ...
- struts深入原理之RequestProcessor与xml
和配置文件相对应的代码(struts1) public void process(HttpServletRequest request, HttpServletResponse response) ...
- EasyUI笔记
以下功能实现朋友们若有更好的思路办法,欢迎留言交流. 1.关闭其他标签页(右键菜单触发,保留左侧第一个欢迎tab) 问题:取到的数组tabs长度会随着tab的关闭而变化 思路:先遍历需要关闭的标签页t ...
- PHP函数处理函数实例详解
1. call_user_func和call_user_func_array: 以上两个函数以不同的参数形式调用回调函数.见如下示例: <?php class AnotherTestClass ...
- Maven使用详解
Maven使用详解 世间万物相生相克,今年本来的目标是主要研究asp.net mvc以及windows平台相关的DI, ORM框架,突然有一天想研究一个Java EE里面相应的框架都是怎么玩的,于是研 ...