使用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]的更多相关文章

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

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

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

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

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

  7. hibernate 异常a different object with the same identifier value was already associated with the session

    在使用hibernate的时候发现了一个问题,记录一下解决方案. 前提开启了事务和事务间并无commit,进行两次save,第二次的时候爆出下面的异常a different object with t ...

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

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

随机推荐

  1. Linux硬盘速度测试的命令

    测试下硬盘的读写速度如何,在linux下可以使用hdparm 对硬盘进行测试或查看硬盘的相关信息. hdparm 命令进行硬盘速度测试.参数: -a 表示是否关闭磁盘预读取功能.对于大文件读取,这个显 ...

  2. atitit。win7 win8 win9 win10 win11 新特性总结与战略规划

    atitit.win7 win8 win9 win10  win11 新特性总结与战略规划 1. win7 1 1.1. 发布时间 2009年10月22日 1 1.2. 稳定性大幅提升,很少蓝屏死机 ...

  3. [svc]nfs客户端报错解决Stale file handle

    NFS故障: 问题背景: 客户端挂载是好的.服务端磁盘满了,重新给挂了一快.客户端df -h 发现nfs挂载消失. 查看目录客户端报错:Stale file handle 现象如下: [root@n1 ...

  4. Linux(Ubuntu/Debian/CentOS/RedHat)下交叉编译boost库

    我用的软件版本如下(其他版本编译方法与此完全相同): Boost Ver: 1.55.0Compiler : GNU gcc 4.6 for ARM 1. 确保ARM编译成功安装,并配置好环境变量.2 ...

  5. MongoDB随笔

    创建用户 db.createUser({user: "abc",pwd: "abc123",roles: [ { role: "readWrite&q ...

  6. mysql实现topN top1

    有时会碰到一些需求,查询分组后的最大值,最小值所在的整行记录或者分组后的top n行的记录,像在hive中是有窗口函数的,可以通过它们来实现,但是MySQL没有这些函数,可通过下面的方法来实现 1.准 ...

  7. Servlet请求转发RequestDispatcher接口

    在Servlet中,利用RequestDispatcher对象,可以将请求转发给另外一个Servlet或JSP页面,甚至是HTML页面,来处理对请求的响应. 一,RequestDispatcher接口 ...

  8. C++-教程3-VS2010C++各种后缀说明

    相关资料:"http://blog.csdn.net/kibaamor/article/details/18700607""http://blog.chinaunix.n ...

  9. [debootstrap]制作基于arm平台的debian文件系统

    之前用过的Linux文件系统是直接busybox制作的,而当前使用的是debian的Jessie,看了一些博客后,了解到如果使用debian,那么直接使用debootstrap来构建文件系统即可 -- ...

  10. linux 驱动cc1101

    cc110x.h /*  * File:   cc110x.h * Author: elinux * * Created on 2015年4月7日, 上午10:32 */ #ifndef CC110X ...