异常信息:

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异常解决方案的更多相关文章

  1. 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 ...异常解决 今天 ...

  2. a different object with the same identifier value was already associat

    问题:这个著名的托管态update更新异常 org.hibernate.NonUniqueObjectException: a different object with the same ident ...

  3. 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 ...

  4. [转]解决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 ...

  5. 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 ...

  6. org.hibernate.NonUniqueObjectException: a different object with the same identifier value was alread---------程序报错

    今天遇到了这个问题: org.hibernate.NonUniqueObjectException: a different object with the same identifier value ...

  7. 解决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错误 这个错 ...

  8. 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"?> ...

  9. 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 ...

随机推荐

  1. So many good projects for studying C programming lanuage.

    Some one asked a question for studying C programming language on stackexachange.com. He got a bucket ...

  2. UI:关于RGB与16进制颜色值的转换

    IOS基本图形绘制 参考 取色对照表 参照 页面背景改为   #5CACEE   的颜色.有两个方法 方法一: 加两个宏 #define UIColorFromHexWithAlpha(hexValu ...

  3. Form实现主从块金额汇总

    1.FORM使用app_calculate.running_total汇总行金额,行上有编码重复验证. 情况一:当录入多个编码重复的行并保存时,报错,清除一个重复行再保存(头行金额一致),报错&quo ...

  4. Cross-Browser HTML5 Placeholder Text

    One of the nice enhancement in HTML5 web form is being able to add placeholder text to input fields. ...

  5. Codeforces Round #338 (Div. 2) C. Running Track dp

    C. Running Track 题目连接: http://www.codeforces.com/contest/615/problem/C Description A boy named Ayrat ...

  6. 从Project 2007导出WBS图表到Visio 2007

    微软官网讲:在 Microsoft Office Project 2007 中,Visio WBS 图表向导已被可视报表代替.您可以使用 Microsoft Office Visio Professi ...

  7. Java和C++中多态的实现方式

    多态是面向对象的最主要的特性之一,是一种方法的动态绑定,实现运行时的类型决定对象的行为.多态的表现形式是父类指针或引用指向子类对象,在这个指针上调用的方法使用子类的实现版本.多态是IOC.模板模式实现 ...

  8. Android端百度地图API使用详解

    百度地图API简介 百度地图移动版API(Android)是一套基于Android设备的应用程序接口,通过该接口,可以轻松的访问百度服务和数据,构建功能丰富.交互性强的地图应用程序. 百度地图移动版A ...

  9. 内存管理和@property的属性

    内存管理和@property的属性 目录 对内存管理的理解 Objective C内存管理方式 内存的管理 对象的所有权和内存管理原则 合理解决内存管理带来的问题 自动释放池 @property的属性 ...

  10. leetcode二分查找问题整理

    自从做完leetcode上的三道关于二分查找的题后,我觉得它是比链表找环还恶心的题,首先能写出bugfree代码的人就不多,而且可以有各种变形,适合面试的时候不断挑战面试者,一个程序猿写代码解决问题的 ...