使用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. ERRORS:<class 'Salesman.admin.UsrMngUserAdmin'>: (admin.E005) Both 'fieldsets' and 'fields' are specified.

    在使用django admin的过程中 遇到了这个错误 . Both 'fieldsets' and 'fields' are specified. django.core.management.ba ...

  2. PHP SOCKET编程(未完)

    转 http://blog.csdn.net/hguisu/article/details/7448528

  3. 如何利用webmin在Linux主机中添加网站

    Linux系统因其高效稳定而受到广大用户的推崇与青睐,然后其管理的复杂性也使很多用户望而却步,动弹不得.为了降低 Linux系统的管理难度,更有效方便的使用该系统,我司所有Linux主机或VPS系统均 ...

  4. Getting Started with Amazon EC2 (1 year free AWS VPS web hosting)

    from: http://blog.coolaj86.com/articles/getting-started-with-amazon-ec2-1-year-free-aws-vps-web-host ...

  5. 关闭IOS更新功能(ios4/5/6)

    防止IOS升级: 工具:ifunbox 展开/System/Library/LaunchDaemons,将下面4个文件删除(不推荐)或者改名(后缀也得改),改名后记得必须重启. com.apple.m ...

  6. 偏于SQL语句的 sqlAlchemy 增删改查操作

    ORM 江湖 曾几何时,程序员因为惧怕SQL而在开发的时候小心翼翼的写着sql,心中总是少不了恐慌,万一不小心sql语句出错,搞坏了数据库怎么办?又或者为了获取一些数据,什么内外左右连接,函数存储过程 ...

  7. ibatis时间比较大小

     <![CDATA[          A.RFID_Time >= #StartTime#          ]]>时间搜索功能 A.RFID_Time <![CDATA[  ...

  8. C++-教程1-VS2010环境设置

    一.需要下载的软件 1.visual studio 2010\\xxzx\tools\编程工具\MICROSOFT\VISUAL.STUDIO\VISUAL.STUDIO.201032位cn_visu ...

  9. RP2837 OUT1-OUT2 对应关系 2路DO

    RP2837 OUT1-OUT2 对应关系: OUT1  AD2   PD22  继电器1 OUT2  AD4   PD24     继电器2 root@sama5d3-linux:~ echo 11 ...

  10. 已知问题汇总 (2013-11-30) - QQ空间, EXTJS

    目前发现两个已知问题暂时无法得到解决: 1. QQ空间问题. 打开页面 http://user.qzone.qq.com/822994792/311, 点击 "xxx人赞" 这个链 ...