异常信息:

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. android 动画属性(一)之Animation

    Animation 在android 程序当中很多时候要用到动画效果,而动画效果主要是Animation来实现的,API给出的解释: 其中包含4种动画效果 AlphaAnimation 渐变透明度 R ...

  2. Server-U_详细配置

    1.首先绿化 Server-U,运行 2.打开Server-U自动弹出如下图:如果不自动弹出,那点击界面上的 新建域  ------ 先有域再有用户,用户在域里面 4. 输入“名称”和“说明”,其中“ ...

  3. [转]Kerberos简介

    Kerberos协议: Kerberos协议主要用于计算机网络的身份鉴别(Authentication), 其特点是用户只需输入一次身份验证信息就可以凭借此验证获得的票据(ticket-grantin ...

  4. Oracle复制表结构和表数据

    一, 复制表结构及数据 create table z_xudebiao_test as select * from v_topic v where v.adddate > to_date('20 ...

  5. javascrpt随笔

    function member(name, gender) { this.name = name; this.gender = gender; this.display = display; //指定 ...

  6. PostgreSQL的AnynonArray的例子

    程序: CREATE OR REPLACE FUNCTION kappend(anynonarray, anyelement) RETURNS text AS $$ ; $$ LANGUAGE SQL ...

  7. Myeclipse如何整合tomcat

    .在本机上安装MyEclipse和Tomcat 5软件程序 2.运行MyEclipse,设置与Tomcat 5服务器的连接,如下图所示: 选择Window--->Preferences,点击进入 ...

  8. ITEXT学习手册

    本系列内容来自<iText in Action 2nd>,希望有时间的读者能够自己读一遍这本书 所有的文章相关的例子:IText.7z ITEXT基础 ITEXT学习手册——创建一个简单的 ...

  9. LVS测试小结

    RedHat5实现负载均衡 http://blog.sina.com.cn/s/blog_4e424e2101007rie.html http://www.doc88.com/p-9975618478 ...

  10. Codeforces Round #336 (Div. 2)A. Saitama Destroys Hotel 水题

    A. Saitama Destroys Hotel 题目连接: http://www.codeforces.com/contest/608/problem/A Description Saitama ...