hibernate的报错信息a different object with the same identifier value was already associated with the session解决办法
废话不多说,直接说原因,这是在hibernate中,有2个相同类型的实体类具有同样的主键标识符,然后调用update或者调用saveOrUpdate,我朋友出这个错的由于他想要update一条数据时,获取主键时从数据库查询获取,此时接收的对象的主键id是12,吧这个值赋给要更新入参的对象,2个对象的主键就都是12了,所有会报错,
@Override
public DTO createExamInfo(DTO dto) { Integer examId = GetParamUtil.convertObject(dto.getParam("examId"));
ExamInfo examInfo=new ExamInfo();
ExamInfo examInfo = examInfoList.get(0); examInfo.setExamId(examId);
Integer examPaperId = GetParamUtil.convertObject(dto.getParam("examPaperId"));
examInfo.setExamPaperId(examPaperId);
String title = GetParamUtil.converObject(dto.getParam("title"));
examInfo.setTitle(title);
Integer pattern = GetParamUtil.convertObject(dto.getParam("pattern"));
examInfo.setPattern(pattern);
Integer type = GetParamUtil.convertObject(dto.getParam("type"));
examInfo.setType(type);
Integer form = GetParamUtil.convertObject(dto.getParam("form"));
examInfo.setForm(form);
Integer monitor = GetParamUtil.convertObject(dto.getParam("monitor"));
examInfo.setMonitor(monitor);
String isPerpetual = GetParamUtil.converObject(dto.getParam("isPerpetual"));
examInfo.setIsPerpetual(isPerpetual);
String startTime = GetParamUtil.converObject(dto.getParam("startTime"));
examInfo.setStartTime(startTime);
String endTime = GetParamUtil.converObject(dto.getParam("endTime"));
examInfo.setEndTime(endTime);
String examCreateTime = GetParamUtil.converObject(dto.getParam("examCreateTime"));
examInfo.setExamCreateTime(examCreateTime);
String createDate = GetParamUtil.converObject(dto.getParam("createDate"));
examInfo.setCreateDate(createDate);
examInfo.setIsUsed(0);
examInfo.setDelFlg("0");
List<ExamInfo> examInfoList=examInfoDao.findByProperty("examId", examId);
if (examInfoList!=null&&examInfoList.size()>0) {
examInfo.setId(examInfoList.get(0).getId());
examInfoDao.update(examInfo);
}else{
examInfoDao.save(examInfo);
} return null;
}
第33行查出了这条数据,此时会话中包含一个id是12的对象(就是此时想要更新的那条数据的主键),然后35行又把id赋值给了另一个对象,执行update时就会报错,我给他改进了一下代码,如下:
@Override
public DTO createExamInfo(DTO dto) { Integer examId = GetParamUtil.convertObject(dto.getParam("examId"));
List<ExamInfo> examInfoList=examInfoDao.findByProperty("examId", examId);
// ExamInfo examInfo=new ExamInfo();
ExamInfo examInfo = examInfoList.get(0); examInfo.setExamId(examId);
Integer examPaperId = GetParamUtil.convertObject(dto.getParam("examPaperId"));
examInfo.setExamPaperId(examPaperId);
String title = GetParamUtil.converObject(dto.getParam("title"));
examInfo.setTitle(title);
Integer pattern = GetParamUtil.convertObject(dto.getParam("pattern"));
examInfo.setPattern(pattern);
Integer type = GetParamUtil.convertObject(dto.getParam("type"));
examInfo.setType(type);
Integer form = GetParamUtil.convertObject(dto.getParam("form"));
examInfo.setForm(form);
Integer monitor = GetParamUtil.convertObject(dto.getParam("monitor"));
examInfo.setMonitor(monitor);
String isPerpetual = GetParamUtil.converObject(dto.getParam("isPerpetual"));
examInfo.setIsPerpetual(isPerpetual);
String startTime = GetParamUtil.converObject(dto.getParam("startTime"));
examInfo.setStartTime(startTime);
String endTime = GetParamUtil.converObject(dto.getParam("endTime"));
examInfo.setEndTime(endTime);
String examCreateTime = GetParamUtil.converObject(dto.getParam("examCreateTime"));
examInfo.setExamCreateTime(examCreateTime);
String createDate = GetParamUtil.converObject(dto.getParam("createDate"));
examInfo.setCreateDate(createDate);
examInfo.setIsUsed(0);
examInfo.setDelFlg("0");
if (examInfoList!=null&&examInfoList.size()>0) {
// examInfo.setId(examInfoList.get(0).getId());
examInfoDao.update(examInfo);
}else{
examInfoDao.save(examInfo);
} return null;
}
直接采用查出数据时接收的对象作为更新时的入参,在这期间给对象set参数,这样从始至终主键为12的对象就只有一个了
hibernate的报错信息a different object with the same identifier value was already associated with the session解决办法的更多相关文章
- 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"?> ...
- 关于报错“More than one fragment with the name [spring_web] was found. This is not legal ...”的解决办法
最近在搭建一个spring mvc 项目时遇到“More than one fragment with the name [spring_web] was found. This is not leg ...
- Centos7最小化安装报错There are no enabled repos. Run "yum repolist all" to see the repos you have.解决办法
原因是缺少CentOS-Base.repo文件,因为我这台机器wget也不能用,所以我是下载到本地sftp上去的,传输的时候一定要在root用户下,否则会无法启动传输 这是报错的完整信息:Loadin ...
- 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 ...
- 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 ...
- 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 th ...
- angular 升级到angular8 以及报错信息解决
1.升级全局angular-cli npm install -g @angular/cli@latest 2.升级项目内 angular-cli (在需要升级的项目中运行) npm i @angula ...
- Hibernate框架报错:org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of com.mikey.hibernate.domain.Person.pid
报错信息 org nate.PropertyAccessException:IllegalArgumentException在调用com.mikey.Hibernate.domain.Person.p ...
- Python 装饰器填坑指南 | 最常见的报错信息、原因和解决方案
本文为霍格沃兹测试学院学员学习笔记. Python 装饰器简介 装饰器(Decorator)是 Python 非常实用的一个语法糖功能.装饰器本质是一种返回值也是函数的函数,可以称之为“函数的函数”. ...
随机推荐
- 【Python】directory字典类型
它的基本格式是(key是键,value是值): d = {key1 : value1, key2 : value2 } Example dir = {'Mic':1,'Sun':2} for k in ...
- QML Delegate中访问该持有者的方式 附加属性(转载)
http://blog.csdn.net/yuxiaohen/article/details/17226971 用法很奇葩记录一下,实测可以,用于弱化delegate与持有者的依赖 delegat ...
- 微软牛津计划——声纹识别与视频识别API上线啦!
上个月,我们发布了牛津计划机器学习的情感识别API,能够帮助不同平台的开发者轻松添加智能应用,而无需精通人工智能领域.牛津计划仅仅是微软在人工智能领域探索中的一个实例,而我们的期望是实现更加注重个人使 ...
- Windows(7)上不能启动MySQL服务(位于本地计算机上)错误1067 :进程意外终止
就这段时间,很多人在抱怨为什么自己的MySQL又打不开问题. 就“Windows(7)上不能启动MySQL服务(位于本地计算机上)错误1067 :进程意外终止”这个问题,我想到了几种方案解决: 一.首 ...
- pt-duplicate-key-checker使用
pt-duplicate-key-checker工具可以检测表中重复的索引,对于一些业务量很大的表,而且开发不规范的情况下有用.基本用法: 看一下我们的测试表: mysql> desc new_ ...
- Day03——Python函数
函数定义和使用 1.语法 def 函数名(参数): ... 函数体 ... 返回值 函数的定义主要有如下要点: def:表示函数的关键字 函数名:函数的名称,日后根据函数名调用函数 函数体:函数中进行 ...
- js中直接调用函数和new函数的区别
如果函数返回值为常规意义上的值类型(Number.String.Boolean)时,new函数将会返回一个该函数的实例对象,而如果函数返回一个引用类型(Object.Array.Function),则 ...
- python3.6安装总结
安装Python 3.6 过程中出现了一些问题,导致费时费力.因此把自己安装过程中出现的问题写出来,以备大家查看. 第一步:比较简单的一种安装方法是直接安装Anacanda3 python即可,这时可 ...
- June 20th 2017 Week 25th Tuesday
Care and diligence bring luck. 谨慎和勤奋,带来好运气. In my opinion, care and diligence may just gurantee us a ...
- GO语言(五)项目搭建
<sorter> |------<src>(手动添加,代码存放处) |------sorter.go |------<algorithm> |--- ...