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() > 0 ){

            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(0);
            ecg.setStationRun(elecCommonMsg.getStationRun());
            ecg.setDevRun(elecCommonMsg.getDevRun());
            ecg.setCreateDate(new Date());

}else{//否则就保存
                elecCommonMsg.setCreateDate(new Date());
                 elecCommonMsgDao.save(elecCommonMsg);
           }
       }

时间: 2024-10-07 06:52:42

Hibernate更新数据报错:a different object with the same identifier value was already associated with the session: [com.elec.domain.ElecCommonMsg#297e35035c28c368015c28c3e6780001]的相关文章

利用struts进行前端页面间传值及hibernate异常:a different object with the same identifier value was already associated with the session的总结

2017-3-16 我使用SSH框架在做单表CRUD的更新操作时遇到了一个问题,就是页面间该怎么传值?解决该需求时引发了一系列的bug,趁还记得好好总结一番. 前端页面间传值 情景:在我查出所以记录后,点击修改会链接到新的修改页面. 问题:该新页面没有之前的实体信息,该如何传递要修改的实体信息给该页面,例如id? 思路1:利用struts的action来传值. 1 <form action="deleteSerCate.action" method="post"

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"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-con

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 already associated with the session异常解决办法 为什么还会说已经存在相同的session了呢.然后每次将项目重启后第一次编辑的时候问题不会触发,只有当第二次操作的时候才会出现这个问题. 解决办法:关闭session.好好检查操作完成后有没有关闭会话. org.hibernat

解决a different object with the same identifier value was already associated with the session错误

20:41:15 今天做一个saveorupdate操作报错: org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was already associated with the session: [com.xshcar.carcloud.entity.UboxTbl#1291]; nested exception is org

[转]解决a different object with the same identifier value was already associated with the session错误

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 already associated with the session:

a different object with the same identifier value was already associated with the session

错误提示: org.springframework.dao.DuplicateKeyException: a different object with the same identifier value was already associated with the session: [cn.itcast.bos.domain.User#4]; nested exception is org.hibernate.NonUniqueObjectException: a different obj

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was alread---------程序报错

今天遇到了这个问题: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:-- 找其原因是因为同一个object,如一个person在seession里保存了一份,而增加的别的object,如company的时候,由于做了关联关系,从数据库里又get了一个person,而这个person和s

Hibernate常见报错

1.A different object with the same identifier value was already associated with the session(使用Hibernate上传数据时,可能会报这个错误) 解决方案:数据库中的表设置了自增,但无效,使用hibernate上传数据时,没有设置主键的id值会报上述错误: 心得:在设置数据库表其他字段的同时,最好将主键id一起设置了. 原文地址:https://www.cnblogs.com/luckyplj/p/101

Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法

最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attribute 'xxx'".这其实是.pyc文件存在问题. 问题定位: 查看import库的源文件,发现源文件存在且没有错误,同时存在源文件的.pyc文件 问题解决方法: 1. 命名py脚本时,不要与python预留字,模块名等相同 2. 删除该库的.pyc文件(因为py脚本每次运行时均会生成.pyc文件