org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was already associated with the session: [cn.ac.iscas.core.entity.pm.cm.CmPlanBaseline#]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [cn.ac.iscas.core.entity.pm.cm.CmPlanBaseline#]
at org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:)
at org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:)
at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:)
at cn.ac.intec.entity.orm.impl.SessionImpl.saveOrUpdate(SessionImpl.java:)
at cn.ac.intec.entity.orm.impl.SessionImpl.insertOrUpdate(SessionImpl.java:)
at cn.ac.intec.entity.orm.impl.SessionImpl$$FastClassByCGLIB$$550f49cd.invoke(<generated>)

触发场景,解决方案:
1. show业务操作,开启一个session ,从中查出一些实体,这时某个实体是存在于session中的持久化对象。通过业务逻辑将此实体用于页面展示。此时这个session并未关闭。

2. 这时执行一步save业务逻辑,将上一步获得的实体id传回后台 。这时通过new的方式创建了一个临时对象,并且我将传回的id作为主键赋值给了临时对象。然后调用了session.save(obj); 方法 。。。抛出异常。

原因很简单,session中有2个oid一样的对象,hibernate不知道该让哪个持久化到库里。当时解决思路也很清晰,直接clear();清空一下 session缓存不就Ok了。但是clear打击面太广了(慎用!)。其他一些“无辜”对象也被杀掉,导致其他业务无法进行了。

后来在session中发现一个evict方法,“定点清除”对象缓存。这下就好了,先用传回来的id用get方法(其实这里用load方法也行反正都是从缓存中加载)获得session里的持久化实体,然后杀掉,再保存临时实体。没问题了。

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:

    hibernate操作: 实例化两个model类,更新时会提示  a different object with the same identifier value was already assoc ...

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

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

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

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

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

  7. Exception 06 : org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session :

    异常名称: org.hibernate.NonUniqueObjectException: A different object with the same identifier value was ...

  8. org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:

    保存实体异常 https://blog.csdn.net/zzzz3621/article/details/9776539 org.hibernate.NonUniqueObjectException ...

  9. org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session异常解决办法

    org.hibernate.NonUniqueObjectException: a different object with the same identifier value was alread ...

随机推荐

  1. mysql 查找多个值并且取最大值一个和分组

    SELECT eco_truename, eco_uid, max(checkup_time) AS time FROM es_checkup_order WHERE checkup_time GRO ...

  2. Ubuntu搭建 Apache、MySQL、PHP环境

    以Ubuntu 16.04为例: 1.安装MysSQL 打开命令行输入 :sudo apt-get install mysql-server 输入管理员密码 选择Y 在安装的中间会出现输入Mysql的 ...

  3. kong nginx 配置文件说明&&借鉴

    备注:     只是简单的进行说明配置文件,不会牵扯到源码   1.  配置文件位置 // 通过ps 查找 ps -ef |grep nginx /usr/local/openresty/nginx/ ...

  4. 搭建一个IntelliJ的Spark项目

    之前发现创建一个新项目之后,无法添加scala class 创建新项目 选择maven项目,然后选择simple或者quickstart: 进入项目后,在Project Structure里面,在gl ...

  5. debian修改连接数限制

    golang写的socket做压力测试的时候,提示too many open files,解决方法如下 sudo gvim /etc/security/limits.conf 添加 * - nofil ...

  6. 解决近期linux下yum更新出现HTTP Error 404 NOT FOUND错误的办法

    本文转载自:http://tech.lezi.com/archives/47 最近两天使用yum的163源,出现404错误 [root@localhost yum.repos.d]# yum make ...

  7. Java文件的写入

    写文件与读文件类似,可以是以字节为单位写入,可以是以字符为单位写入. 对应读操作FileOutputStream是以字节为单位进行写入的: FileOutputStream fileOutputStr ...

  8. 利用nginx_push_stream_module实现服务器消息推送

    NGiNX_HTTP_Push_Module 是一个 Nginx 的扩展模块,它实现了 HTTP Push 和Comet server的功能.HTTP Push 被经常用在网页上主动推的技术,例如一些 ...

  9. MySQL添加数据库的唯一索引的几种方式~

    创建表时直接设置: DROP TABLE IF EXISTS `student`;CREATE TABLE `student` (  `stu_id` int(11) NOT NULL AUTO_IN ...

  10. socket编程之select()

    int select(int maxfdp,fd_set *readfds,fd_set *writefds,fd_set *errorfds,struct timeval *timeout); 参数 ...