使用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. CTC loss 理解

    参考文献 CTC学习笔记(一) 简介:https://blog.csdn.net/xmdxcsj/article/details/51763868 CTC学习笔记(二) 训练和公式推导 很详细的公示推 ...

  2. 关闭windows打印服务

    1.关闭打印服务:开始-运行-services.msc或打开控制面板-管理工具-服务,打开服务列表,找到Print Spooler(打印服务),关闭(右击,点“关闭”).2.删除打印缓存:进入c:\\ ...

  3. Memory leak patterns in JavaScript

    Handling circular references in JavaScript applications Plugging memory leaks in JavaScript is easy ...

  4. [原]C#设置文件夹用户权限

    var security = new DirectorySecurity();   string path=@"C:\temp" //设置权限的应用为文件夹本身.子文件夹及文件,所 ...

  5. 随机用户id号,随机密码用户名

    类似新浪微博的用户Id怎么生成呢? 特点:10位随机数,而且是以1开头的 好处:不容易猜出有多少用户 方法一: 目的是生成唯一id.可以用uniqid.uniqid获取一个字符串,循环这个字符串,把每 ...

  6. SimpleDateFormat线程不安全及解决的方法

    一. 为什么SimpleDateFormat不是线程安全的? Java源代码例如以下: /** * Date formats are not synchronized. * It is recomme ...

  7. CSS3边框圆角知识

    <div class="item" data-brief="整圆"> <div class="border-radius" ...

  8. 实现在edittext中任意插入图片

    Myedittext: public class MyEditText extends EditText { public MyEditText(Context context) { super(co ...

  9. Spider Studio 新版本 (码年吉祥版) - 浏览器视图 / 脚本库上线!

    各位哥哥姐姐弟弟妹妹小伙伴们春节好! 2014年对于我们程序员很重要, 因为今年是 "码" 年! SS在此重要之年到来之际热力推出两大重要功能恭贺新春: 1. 浏览器视图 以前SS ...

  10. 2015 Multi-University Training Contest 3 1002 RGCDQ

    RGCDQ Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5317 Mean: 定义函数f(x)表示:x的不同素因子个数. 如:f ...