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 ...
随机推荐
- 【转】Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置)
Android-Universal-Image-Loader 原文地址:http://blog.csdn.net/vipzjyno1/article/details/23206387 这个图片异步加载 ...
- MFC中 Invalidate() , InvalidateRect() , UpdateWindow(), Redrawwindow() 区别
1. void Invalidate( BOOL bErase = TRUE ); 该函数的作用是使整个窗口客户区无效.窗口的客户区无效意味着需要重绘,例如,如果一个被其它窗口遮住的窗口变成了前台窗口 ...
- Mysql的存储过程(以Mysql为例进行讲解)
我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集,经编译后存储 在数据库中,用户通过指定存 ...
- js原生appendChild的bug
appendChild 主要是用来追加节点 插入到最后 window.onload = function(){ var ul2 = document.getElementById('ul2'); va ...
- Softmax 回归原理介绍
考虑一个多分类问题,即预测变量y可以取k个离散值中的任何一个.比如一个邮件分类系统将邮件分为私人邮件,工作邮件和垃圾邮件.由于y仍然是一个离散值,只是相对于二分类的逻辑回归多了一些类别.下面将根据多项 ...
- 企业应用架构模式阅读笔记 - Martin Fowler
1. 数据读取
- 导入word数据
public static List<FL> ImportDOC(object fileName,out StringBuilder meg) { List<FL> ...
- 实现输出h264直播流的rtmp服务器 flash直播服务器【转】
实现输出h264直播流的rtmp服务器 RTMP(Real Time Messaging Protocol)是常见的流媒体协议,用来传输音视频数据,结合flash,广泛用于直播.点播.聊天等应用,以及 ...
- [CoffeeScript] Level 4 Arrays, Objects, Iterations -- Ex
Coffee on the Range Create an array with numbers 1 until 10 using the inclusive (two dot) range synt ...
- Python基础教程之第2章 列表和元组
D:\>python Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Typ ...