[转]解决a different object with the same identifier value was already associated with the session错误
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)
以上两中异常经常出现在一对多映射和多对多映射中
举一下自己的代码
/**
* 取得当前Session.
* @return Session
*/
public Session getSession() {
return sessionFactory.getCurrentSession();
} /**
* 保存新增或修改的对象.
* @param entity
*/
public void save(final T entity) {
getSession().saveOrUpdate(getSession().merge(entity));
}
[转]解决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中一种导致a different object with the same identifier value was already associated with the session错误方式及解决方法
先将自己出现错误的全部代码都贴出来: hibernate.cfg.xml <?xml version="1.0" encoding="UTF-8"?> ...
- 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 thesession时,一般是因为在h ...
- 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 ...
- 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 assoc ...
- 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 ...
- a different object with the same identifier value was already associated with the session解决方案
org.springframework.orm.hibernate3.HibernateSystemException: a different ]; nested exception ] at or ...
- 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 ...
随机推荐
- $.post() 传递多个参数.
$("#button").click(function() { /获取表单中id为idname和count的文本值付给property的两个属性 var property={&qu ...
- ASP.NET MVC5 实现网址伪静态
一.路由规则: routes.MapRoute( name: "Default1", url: "more_{root}_{plate}.html", defa ...
- range()函数的使用
坚持每天学一点,每天进步一点,迟早有一点我会成为大神. 在python中range函数可以返回一系列连续增加的整数,也是一个迭代器. 函数用法:range(开始, 结束, 步进值): #步进值默认为1 ...
- 使用USBWriter做U盘启动盘后容量变小的解决办法
环境windows10 , 以administrator登录: 1. 按下windows键盘上的Win+R键, 输入cmd 2. 输入DiskPart, 回车, 然后弹出新的命令行窗口 3. 在弹出 ...
- 5G扫盲
打开手机,连接wifi,如果想要下载一部自己喜欢的高清电影,一般需要半小时左右的时间.5G时代,你可能花费不到1分钟就能完成这些操作.更快的网速.更多的应用意味着更广阔的市场空间,也意味着更激烈的市场 ...
- Imagenet tools install on windows
1.find the pyrcc4.exe path: C:\Anaconda2\Library\bin 2.cmd: pyrcc4 -o resources.py resources.qrc 3.a ...
- .NET微信公众号开发-6.0模板消息
一.前言 为了保证用户不受到骚扰,在开发者出现需要主动提醒.通知用户时,才允许开发者在公众平台网站中模板消息库中选择模板,选择后获得模板ID,再根据模板 ID向用户主动推送提醒.通知消息.这个意思也就 ...
- [LeetCode] 8. String to Integer (atoi)
Implement atoi to convert a string to an integer. public class Solution { public int myAtoi(String s ...
- BestCoder Round #90 A.Kblack loves flag(随机数生成种子)
A.Kblack loves flag [题目链接]A.Kblack loves flag [题目类型]水题 &题意: kblack喜欢旗帜(flag),他的口袋里有无穷无尽的旗帜. 某天,k ...
- [Linux] 无法访问国外网站,完成epel源安装的解决办法--待续
一.缘由: 由于一个机房的网络限制,无法访问国外IP地址,在安装一些开源软件的时候比如smokeping.ansible就无法从epel源在线安装, 编译安装的话,又需要安装各种依赖,麻烦的一逼.所以 ...