Hibernate更新数据报错:a different object with the same identifier value was already associated with the session: [com.elec.domain.ElecCommonMsg#297e35035c28c368015c28c3e6780001]
使用hibernate更新数据时,报错
Struts has detected an unhandled exception:
Messages:
a different object with the same identifier value was already associated with the session: [com.elec.domain.ElecCommonMsg#297e35035c28c368015c28c3e6780001]
a different object with the same identifier value was already associated with the session: [com.elec.domain.ElecCommonMsg#297e35035c28c368015c28c3e6780001];
nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:
[com.elec.domain.ElecCommonMsg#297e35035c28c368015c28c3e6780001]
代码如下:
/**
* @Name saveElecCommonMsg
* @Description: 保存运行监控表的数据
* @author kj
* @version: V1.00
* @create Date: 2017-05-21
* @Parameters: saveElecCommonMsg:VO对象
* @return 无
* 此处需要保存或者更新,要加上事务
*/
@Override
@Transactional(readOnly=false)
public void saveElecCommonMsg(ElecCommonMsg elecCommonMsg) {
// 1.查询数据库运行监控表的数据,返回List,用来判断数据是否存在
List<ElecCommonMsg> list = elecCommonMsgDao.findCollectionByConditionNoPage("", null, null);
//存在就更新
if(list != null && list.size() > ){ ElecCommonMsg cmg = list.get(0);
elecCommonMsg.setComID(cmg.getComID());
elecCommonMsg.setCreateDate(new Date());
elecCommonMsgDao.update(elecCommonMsg);
}
}
原因是:使用update更新(Session中不允许出现2个相同的OID),所以此处改用 使用快照进行更新
/**
* @Name saveElecCommonMsg
* @Description: 保存运行监控表的数据
* @author kj
* @version: V1.00
* @create Date: 2017-05-21
* @Parameters: saveElecCommonMsg:VO对象
* @return 无
* 此处需要保存或者更新,要加上事务
*/
@Override
@Transactional(readOnly=false)
public void saveElecCommonMsg(ElecCommonMsg elecCommonMsg) {
// 1.查询数据库运行监控表的数据,返回List,用来判断数据是否存在
List<ElecCommonMsg> list = elecCommonMsgDao.findCollectionByConditionNoPage("", null, null);
//存在就更新
if(list != null && list.size() > 0 ){
ElecCommonMsg ecg = list.get();
ecg.setStationRun(elecCommonMsg.getStationRun());
ecg.setDevRun(elecCommonMsg.getDevRun());
ecg.setCreateDate(new Date());
}else{//否则就保存
elecCommonMsg.setCreateDate(new Date());
elecCommonMsgDao.save(elecCommonMsg);
}
}
Hibernate更新数据报错:a different object with the same identifier value was already associated with the session: [com.elec.domain.ElecCommonMsg#297e35035c28c368015c28c3e6780001]的更多相关文章
- 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 ...
- 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 ...
- hibernate 异常a different object with the same identifier value was already associated with the session
在使用hibernate的时候发现了一个问题,记录一下解决方案. 前提开启了事务和事务间并无commit,进行两次save,第二次的时候爆出下面的异常a different object with t ...
- [转]解决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 ...
随机推荐
- atitit.抽奖活动插件组件设计--结构设计and 抽奖流程建模
atitit.抽奖活动插件组件设计--结构设计and 抽奖流程建模 1. 组件结构 1 2. startDraw 开始抽奖流程建模 1 3. 抽奖算法 2 作者:: 老哇的爪子 Attilax 艾龙, ...
- cygwin 运行java的一些记录
javac编译没问题,但是java执行就不行,处理方式如下 其实就是把宿主机下的java.exe在cygwin下搞一个快捷方式 关于路径问题,可以使用cygpath命令进行宿主机和cygwin间的转换 ...
- 使用nio实现web服务器
package com.nio; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocket ...
- linux下tar.gz、tar、bz2、zip等解压缩、压缩命令小结(转载)
Linux下最常用的打包程序就是tar了,使用tar程序打出来的包我们常称为tar包,tar包文件的命令通常都是以.tar结尾的.生成tar包后,就可以用其它的程序来进 行压缩了,所以首先就来讲讲ta ...
- linux服务器 IE中ico 不能正常显示
问题: mime_type: image/vnd.microsoft.icon 的,但发现在 IE 下面,直接打开 icon 的地址,图标不能正常显示 1.将ico放在windows服务器上,直接访问 ...
- oracle初始操作
oracle登录 sqlplus sys/oracle as sysdba 这个登录之后呢 会出现这个: Connected to an idle instance. 这一步是连接上 [oracle ...
- mysql索引学习
索引用于快速找出在某列中有一特定值的行. 如果不使用索引,MySQL必须从第一条记录开始读完整个表,直到找出相关的行. 表越大,查询数据所花费的时间越多. 如果表中查询的列有一个索引,MySQL能快速 ...
- am335x 无屏实现开关机程序
因测试需要加入开机次数记录,所以记录一下7816开关机是怎么做的 原理很简单,开机时判断一个记录文件是否存在,如果存在,运行一段代码,将记录开机次数文件的值读出来+1 代码如下: #include & ...
- Scala中集合类型与java中集合类型转换
对于java中的集合元素并不能在scala中拿来就用的,需要进行相应的转换. 1. 转换规则如下 从下面可以看出,有些可以相互转换的,有些只能单向转换: scala.collection.Iterab ...
- Hive及HBase数据迁移
一. Hive数据迁移 场景:两个Hadoop平台集群之间Hive表迁移. 基本思路:Hive表元数据和文件数据export到HDFS文件,通过Distcp将HDFS迁移到另一个集群的HDFS文件,再 ...