a different object with the same identifier value was already associated withthe session异常解决方案
异常信息:
org.hibernate.NonUniqueObjectException:
a different object with the same identifier value was already associated withthe session
发生原因:
在对一个实体查询操作后,进行了set,然后又要对这个实体进行save/update,
报错的例子:
//Service层
public void modifyPersonalizedSettings(PersonalizedSettings ps,Integer userId)throws Exception { User user = personalizedSettingsDAO.selectUserById(userId);
ps.setUser(user);
personalizedSettingsDAO.updatePersonalizedSettings(ps);
}
//DAO层
public void updatePersonalizedSettings(PersonalizedSettings ps) throws Exception {
System.out.println(ps+"DAO");
template.merge(ps);
}
解决方案:
最简单的就是不用save()方法,改用merge()方法。
例子:
//Service层
public void modifyPersonalizedSettings(PersonalizedSettings ps,Integer userId)throws Exception { User user = personalizedSettingsDAO.selectUserById(userId);
ps.setUser(user);
personalizedSettingsDAO.updatePersonalizedSettings(ps);
}
//DAO层
public void updatePersonalizedSettings(PersonalizedSettings ps) throws Exception {
System.out.println(ps+"DAO");
//template.update(ps)不再使用update()方法,改用merge()方法即可
template.merge(ps);
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
a different object with the same identifier value was already associated withthe session异常解决方案的更多相关文章
- a different object with the same identifier value was already associated with **(ssh异常转)
Hibernate:a different object with the same identifier value was already associated with ...异常解决 今天 ...
- a different object with the same identifier value was already associat
问题:这个著名的托管态update更新异常 org.hibernate.NonUniqueObjectException: a different object with the same ident ...
- 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 ...
- [转]解决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. 错误原因:在h ...
- 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 alread---------程序报错
今天遇到了这个问题: org.hibernate.NonUniqueObjectException: a different object with the same identifier value ...
- 解决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"?> ...
- 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 ...
随机推荐
- java去掉jsp标签内容的方法
//去掉内容的标签 public static String removeTag(String count){ try { int tagCheck=-1; do { i ...
- 转载:你可能没注意的CSS单位
出自:http://www.cnblogs.com/dolphinX/p/4097693.html 扶今追昔 CSS中的单位我们经常用到px.pt.em.百分比,px和pt不用多说 em em是相对单 ...
- jQuery(一)delegate() 方法
定义和用法 delegate() 方法为指定的元素(属于被选元素的子元素)添加一个或多个事件处理程序,并规定当这些事件发生时运行的函数. 使用 delegate() 方法的事件处理程序适用于当前或未来 ...
- center os 6.5 vsftpd 登陆出现 530 错误拒绝 解决方法
别管那么多 把 /etc/vsftpd/ftpusers 里面的用户名删掉就好了.
- jQuery.FlexiGrid使用总结
经过对FlexiGrid的大量使用,及时不时琢磨下其代码,对她的脾性有了一定的了解,是该做总结的时候了. 一.FlexiGrid下载 1.原版代码 最近Paulo P. Marinas对FlexiGr ...
- hadoop集群扩展
一.先改动master机上的host文件,新增内容 10.61.6.180 slaves15 二.改动master机上hadoop安装文件夹下的conf中的slaves文件.新增内容 slaves15 ...
- Java虚拟机工作原理具体解释
一.类载入器 首先来看一下java程序的运行过程. 从这个框图非常easy大体上了解java程序工作原理.首先,你写好java代码,保存到硬盘其中.然后你在命令行中输入 javac YourClass ...
- ext2文件系统
2.1. 总体存储布局 我们知道,一个磁盘可以划分成多个分区,每个分区必须先用格式化工具(例如某种mkfs命令)格式化成某种格式的文件系统,然后才能存储文件,格式化的过程会在磁盘上写一些管理存储布局的 ...
- SQL Sever 2008 数据库文件的备份和还原
数据库文件备份保存的方式我知道的有两种,第一种就是我们知道到的简单的分离,生成.mdf和.ldf文件,然后使用的时候直接进行附加数据库就可以了. 那么还有生成备份的文件.bak 的,这个操作要在步骤: ...
- PMP学习感想
经过一个多月的漫长等待,伴随着焦虑与激动,终于迎来了2016新年的第一个惊喜,PMP考试通过了! 三个多月的努力,利用休息时间,终于把这块硬骨头给啃了下来. 感谢慧翔天地的老师和学友们,是在你们的努力 ...