码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2017-05-21 12:58:02      阅读:334      评论:0      收藏:0      [点我收藏+]

标签:mon   nested   condition   action   date   快照   author   getc   set   

使用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);
           }
       }

 

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

标签:mon   nested   condition   action   date   快照   author   getc   set   

原文地址:http://www.cnblogs.com/taiguyiba/p/6884356.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!