异常信息:

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. webbench压力测试

    webbench最多可以模拟3w多个并发请求去测试网站负载能力. 一:获取webbench工具,安装编译: wget http://blog.zyan.cc/soft/linux/webbench/w ...

  2. 集成StyleCop到Jenkins CI

    这是集成完stylecop之后的Jenkins,可以看到code review结果随每个build变化的图表,Build History里面可以看到#150之前的build状态是unstable,这是 ...

  3. BeanFactory和ApplicationContext的作用和区别

    BeanFactory和ApplicationContext的作用和区别 作用: 1. BeanFactory负责读取bean配置文档,管理bean的加载,实例化,维护bean之间的依赖关系,负责be ...

  4. Asp.net使用jQuery实现数据绑定与分页

    使用jQuery来实现Gridview, Repeater等服务器端数据展示控件的数据绑定和分页.本文的关注重点是数据如何实现数据绑定. Content jQuery的强大和可用性使得其迅速的流行起来 ...

  5. 模拟log4j获取日志对象调用所在的类名、方法名及行号

    当我们在记录日志时,每个类中会定义一个日志对象,然后利用这个对象去写日志,那么我们在处理日志时,如何能才能记录日志对象所在的类.方法和行号呢?log4j中已经实现了该功能,那么它是怎么实现的呢? 其实 ...

  6. 使用generator自动生成Mybatis映射配置文件

    在使用mybatis时,映射文件的配置非常麻烦,对于做逻辑不是很复杂,功能不是特别关键的模块的时候,我们没有必要手动书写,可以使用generator工具生成. generator工具实际上就是根据数据 ...

  7. 【Hybrid App】Hybrid App开发实战

    [引言]近年来随着移动设备类型的变多,操作系统的变多,用户需求的增加,对于每个项目启动前,大家都会考虑到的成本,团队成员, 技术成熟度,时间,项目需求等一堆的因素.因此,开发App的方案已经变得越来越 ...

  8. 基数树(radix tree)

    原文   基数(radix)树 Linux基数树(radix tree)是将指针与long整数键值相关联的机制,它存储有效率,并且可快速查询,用于指针与整数值的映射(如:IDR机制).内存管理等.ID ...

  9. sqlserver 行转列 语文,数学,物理,化学

    数据库查询行转列 1.原数据库值 stdname stdsubject result 张三 语文 张三 数学 张三 物理 李四 语文 李四 数学 李四 物理 李四 化学 李四 化学 2.要得到如下表 ...

  10. 小白日记48:kali渗透测试之Web渗透-XSS(二)-漏洞利用-键盘记录器,xsser

    XSS 原则上:只要XSS漏洞存在,可以编写任何功能的js脚本 [反射型漏洞利用] 键盘记录器:被记录下的数据会发送到攻击者指定的URL地址上 服务器:kali 客户端 启动apache2服务:ser ...