a different object with the same identifier value was already associated with the session解决方案
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解决方案的更多相关文章
- 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 ...
- [转]解决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 ...
- 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 ...
- 解决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错误 这个错 ...
- 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"?> ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- objectForKey与valueForKey在NSDictionary中的差异
从 NSDictionary 取值的时候有两个方法,objectForKey: 和 valueForKey:,这两个方法具体有什么不同呢? 先从 NSDictionary 文档中来看这两个方法的定义: ...
- [MEF]第03篇 MEF延迟加载导出部件及元数据
一.演示概述此演示介绍了MEF的延迟加载及元数据相关的内容.在实际的设计开发中,存在着某些对象是不需要在系统运行或者附属对象初始化的时候进行实例化的,只需要在使用到它的时候才会进行实例化,这种方式就可 ...
- 推荐PHP程序员进阶的好书
<UNIX网络编程卷1(第3版)> <UNIX网络编程卷2(第2版)> <UNIX环境高级编程(第3版)> <UNIX编程艺术> <MySQL技术 ...
- 让html标签显示在页面上
用 <xmp></xmp> 标签包起来,里面的所有文字会原样显示出来 <xmp><b>1</b><div>2</div&g ...
- Paramiko,数据库
Paramiko 该模块基于SSH用于连接远程服务器并执行相关操作 SSHClient 用于连接远程服务器并执行基本命令 import paramiko # 创建SSH对象 ssh = paramik ...
- java代码-------继承的方法----重写还是重载
总结:是自己不听讲吧,不懂啊 感觉父类的方法,子类可以重载,只要参数个数不同,重载与返回值没有关系 重写绝对是可以的.但答案是只能重写啊 package com.s.x; public class T ...
- Condition分析
Condition中提供了一组类似于Object中的监视器方法.与Lock配合可以完成等待通知模式. Condition只能通过Lock#newCondition()方法获取,所以Condition是 ...
- CentOS7 php7 安装 curl 扩展
直接从php源码包中,使用root权限安装. 找到原先安装PHP的源码包文件(如果已经删掉需要重新下载原来版本的源码包并解压) 我的php源码包在root家目录下. cd /php-7.1.4/ext ...
- Go - 类型与变量
类型 Go 语言中的类型与其他语言类似,比较特殊的有以下几个: bool 类型 - 它的值只能是 true 与 false. int / uint - 它们的长度会根据操作系统的不同(32/64 bi ...
- python学习 (二十八) Python的for 循环
1: for 循环可以循环如下类型: my_string = "abcabc" // 字符串类型 for c in my_string: print(c, end=' ') car ...