a different object with the same identifier val...
2019独角兽企业重金招聘Python工程师标准>>>
a different object with the same identifier value was already associated with the session
一个经典的hibernate错误:a different object with the same identifier value was already associated with the session xxxx
hibernate3.0以上使用merge()来合并两个session中的同一对象
具体到我自己的代码就是
public Object getDomain(Object obj) {
getHibernateTemplate().refresh(obj);
return obj;
}
public void deleteDomain(Object obj) {
obj = getHibernateTemplate().merge(obj);
getHibernateTemplate().delete(obj);
}
解决a different object with the same identifier value was already associated with the session错误
这个错误我一共遇到过两次,一直没有找到很好的解决方案,这个错误产生原因相信大家都知道,因为在hibernate中同一个session里面有了两个相同标识但是是不同实体,当这时运行saveOrUpdate(object)操作的时候就会报这个错误。呵呵,也许你会说,你这么说跟没说没什么区别,我承认,呵呵,我不知道具体为什么会产生这个错误,要不然也不会很久都没有解决,现在,给出一个临时的解决方案,给向我一样,没有办法找到根源的人一个能够继续执行下去的方法(当然是对的,只是不是从产生原因入手)
其实要解决这个问题很简单,只需要进行session.clean()操作就可以解决了,但是你在clean操作后面又进行了saveOrUpdate(object)操作,有可能会报出"Found two representations of same collection",我找了很多资料,没有什么很好的解释,其中这篇文章帮助最大[url]http://opensource.atlassian.com/projects/hibernate/browse/HHH-509[/url]。
最后通过session.refresh(object)方法就可以解决了,注意,当object不是数据库中已有数据的对象的时候,不能使用session.refresh(object)因为refresh是从hibernate的session中去重新取object,如果session中没有这个对象,则会报错所以当你使用saveOrUpdate(object)之前还需要判断一下
当然这个问题最容易解决的办法还是使用Hibernate里面自带的merge()方法。不过我始终觉得碰到问题就用这种软件自带的非常用方法(和saveOrUpdate(),save(),update()相比)感觉十分不爽。
后来我还发现这种错误经常出现在一对多映射和多对多映射,请大家在使用一对多和多对多映射的时候要小心一些
Hibernate 疑难异常及处理
1、a different object with the same identifier value was already associated with the session。
错误原因:在hibernate中同一个session里面有了两个相同标识但是是不同实体。
解决方法一:session.clean()
PS:如果在clean操作后面又进行了saveOrUpdate(object)等改变数据状态的操作,有可能会报出"Found two representations of same collection"异常。
解决方法二:session.refresh(object)
PS:当object不是数据库中已有数据的对象的时候,不能使用session.refresh(object)因为该方法是从hibernate的session中去重新取object,如果session中没有这个对象,则会报错所以当你使用saveOrUpdate(object)之前还需要判断一下。
解决方法三:session.merge(object)
PS:Hibernate里面自带的方法,推荐使用。
2、Found two representations of same collection
错误原因:见1。
解决方法:session.merge(object)
以上两中异常经常出现在一对多映射和多对多映射中
a different object with the same identifier value was already associated with the session
一个经典的hibernate错误:a different object with the same identifier value was already associated with the session xxxx
hibernate3.0以上使用merge()来合并两个session中的同一对象
具体到我自己的代码就是
public Object getDomain(Object obj) {
getHibernateTemplate().refresh(obj);
return obj;
}
public void deleteDomain(Object obj) {
obj = getHibernateTemplate().merge(obj);
getHibernateTemplate().delete(obj);
}
try {
Session session = this.openSession();
beginTransaction();
persistentObject = session.merge(persistentObject);
Serializable id = session.save(persistentObject);
if (autoCommit)
commitTransaction();
return id;
} catch (HibernateException ex) {
log.error("Fail to save persistentObject", ex);
throw new DaoException("Fail to save persistentObject", ex);
} finally {
if (autoCloseSession)
closeSession();
}
}
转载于:https://my.oschina.net/u/617245/blog/71885
a different object with the same identifier val...相关推荐
- a different object with the same identifier value was already associated with the session
当出现a different object with the same identifier value was already associated with the session时,一般是因为在 ...
- a different object with the same identifier value was already associated with the session:
hibernate操作: 实例化两个model类,更新时会提示 a different object with the same identifier value was already associ ...
- 关于出现org.hibernate.TransientObjectException: The given object has a null identifier: 错误的解决方法
关于出现org.hibernate.TransientObjectException: The given object has a null identifier: 错误的解决方法 参考文章: (1 ...
- a different object with the same identifier value was already associated with the session解决方案
a different object with the same identifier value was already associated with the session解决方案 参考文章: ...
- org.hibernate.transientobjectexception:The given object has a null identifier: com.gxuwz.check.entit
错误信息: org.hibernate.TransientObjectException: The given object has a null identifier: com.gxuwz.chec ...
- 解决a different object with the same identifier value was already associated with the session错误...
问题场景: (我要做对象修改,然后保存进数据库) 我通过数据库获取一个原对象obj; 然后新增了一个修改对象obj1; 然后将obj1.setId(obj.getId()); 然后调用数据库实例对象S ...
- a different object with the same identifier value was already associated whith
问题:在调用spring的getHibernateTemplate().saveOrUpdate()方法时 a different object with the same identifier va ...
- 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 ...
- NB IoT LWM2M Object or/and Resource Identifier: (Object ID) or (ResourceID)
NB IoT LWM2M Object or/and Resource Identifier: (Object ID) or (ResourceID) LWM2M协议中Object ID-对 ...
最新文章
- 通过Windows DShow获取设备名、支持的编解码及视频size列表实现
- java协变 生产者理解_Java进阶知识点:协变与逆变
- Tomcat中session的管理机制
- apscheduler mysql_APScheduler (重点)
- 【VBA】多条件去重
- raspberry pi_您应该为项目选择哪种Raspberry Pi?
- js读取txt文件中的内容
- LabVIEW编程LabVIEW开发 施耐德
- matlab 彩色图片分解,MATLAB 彩色图像分割
- python数据处理方法
- Acer传奇Go电脑开机自动安装软件卡死怎么重装系统?
- 【一周头条盘点】中国软件网(2018.2.26~2018.3.2)
- Python 爬虫实战(1):分析豆瓣中最新电影的影评并制作词云
- 程序员加班面临的问题
- 一名渗透工程师所必备的技能(一)
- 技嘉b365m小雕驱动工具_百元也有“雕”牌!技嘉B365M Aorus Elite主板评测
- 数据仓库-主数据管理
- [附下载]功能最强、高效易用的AI自动标注软件免费用了,兼容LabelImg格式xml,支持各种yolo、ssd、rcnn已训练模型以及OCR、形状匹配、轮廓匹配等各种定位方式来自动生成标注文件
- [torchtext]如何利用torchtext读取json文件并生成batch
- 通过贝叶斯公式计算阳性概率