异常信息:

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. keycode按键对照表

    功能场景,鼠标在某区域内,比如多个条件的搜索框内,按下enter键让其具有,点击 [确定/搜索]按钮的功能.在编辑的区域内,点击enter键,让其有 [保存]按钮的功能.则可这样:$("#s ...

  2. CentOS7 安装 MySQL 5.7.10

    卸载冲突的rpm包:rpm -qa|grep mariadbrpm -e --nodeps mariadb-libs-5.5.41-2.el7_0.x86_64 安装:rmp -ivh mysql-c ...

  3. C:结构体

    结构体 构造类型:就是有基本的类型组成的 1.结构体  结构体是一种自定义的数据类型 和 int float 是一样的都可以定义变量 数组 只能存放一种类型的容器 结构体   可以存放多种数据类型  ...

  4. HTML第五天学习笔记

    今天先是学习了基础的css样式 <html> <head> <title></title> <meta http-equiv = "co ...

  5. cocos2d-x 用浏览器打开网页

    转自:http://www.xuebuyuan.com/1396292.html,http://www.cocoachina.com/bbs/read.php?tid=88589 First!! 源代 ...

  6. IC开短路测试(open_short_test),编程器测试接触不良、开短路

    http://kitebee.meibu.com/forum.php?mod=viewthread&tid=69654&extra=page%3D5 IC开短路测试(open_shor ...

  7. 用javascript实现简体和繁体字间的转换

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  8. LVS测试小结

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

  9. 使用Url.Routeurl获取url值。

    1,获取url值.         public ActionResult About()         {            RouteValueDictionary RVD = new Ro ...

  10. oc-25-id类型

    /** id:万能指针 能够指向任何OC对象. id = NSObject *, id cat = [Cat new]; [cat jump]; NSObject:是所有类的父类,基类.可以指向任何O ...