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 非常实用的一个语法糖功能.装饰器本质是一种返回值也是函数的函数,可以称之为“函数的函数”. ...
随机推荐
- Codeforces Round #411 A. Fake NP
A. Fake NP time limit per test 1 second memory limit per test 256 megabytes Tavak and Seyyed a ...
- 03_Adaptive注解
[Adaptive注解] package com.alibaba.dubbo.common.extension; import com.alibaba.dubbo.common.URL; import ...
- BEM,SASS,LESS,bootstrap:如何有效地将这些方法,工具和框架聪明地整合?
https://medium.com/@andersonorui_/bem-sass-and-bootstrap-9f89dc07d20f Bootstrap是一个“HTML,CSS和Javascri ...
- POI读取xls和xlsx
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import ...
- redis持久化方法
1.redis持久化,来自官方说明 如何选择使用哪种持久化方式? 一般来说, 如果想达到足以媲美 PostgreSQL 的数据安全性, 你应该同时使用两种持久化功能. 如果你非常关心你的数据, 但仍然 ...
- eclipse通过maven进行打包并且对hdfs上的文件进行wordcount
在eclipse中配置自己的maven仓库 1.安装maven(用于管理仓库,jar包的管理) -1.解压maven安装包 -2.把maven添加到环境变量/etc/profile -3.添加mave ...
- Struts2学习-ssh框架
SSH是 struts+spring+hibernate的一个集成框架,是目前比较流行的一种Web应用程序开源框架. http://www.cnblogs.com/laibin/p/5847111.h ...
- dailiaojie new
http://imushan.com/categories/Java/ 编译优化手段.
- 名词后变为复数+s,或者+es等怎么读
, 以ce,se,ze, (d)ge等结尾的词 加 -s 读 /iz/ license-licenses, office offices 最佳答案1: 当名词后加-e(-es)变成复数,动词单数第三人 ...
- UID卡修改&UID锁死修复
好久没发RFID类文章,最近有小伙伴问到UID卡的问题,在这里就写一写吧. 首先是UID修改的问题,只要卡是UID卡,就都可以修改UID,首先读卡器连接电脑,卡片放到读卡器上. 然后我们要用一个工具, ...