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 非常实用的一个语法糖功能.装饰器本质是一种返回值也是函数的函数,可以称之为“函数的函数”. ...
随机推荐
- How to use Log4cplus
Introduction Log4cplus is derived by the popular Log4j written in java.<br>This tutorial show ...
- HTML5+CSS3开发移动端页面
前提知识: 1.能够独立根据设计稿进行整套项目的需求.剖析及其开发: 2.对项目开发流程需要有一个基本的了解: 3.可以灵活运用切图.重构.前端的知识对项目进行灵活控制. 开发步骤之需求分析: 1.确 ...
- windows c++ 修改用户的文件夹操作权限
一般Windows下的系统文件(夹)只让受限帐户读取而不让写入和修改.如果要开启写操作权限就需要手动修改文件(夹)的用户帐户安全权限(这操作当然要在管理员帐户下执行).以下用程序封装了一下该操作: # ...
- SQL Server ->> OFFSET & FETCH子句
SQL Server 2012引入OFFSET + FETCH字句.它俩出现在SELECT .... ORDER BY ...后面.作用是告诉SQL Server在结果集中忽略前N行然后取前M行出来. ...
- Android已上线应用开源分享中(第二季)
昨天和大家分享了我Android上线的第一个应用,大家还是挺支持的,很高兴,虽然作品没有那么高大上,但是很是有了一点小小的成就感,所以打算继续开源我上线的一些应用,和大家一起交流一下. 我这个作品是一 ...
- ss.c
linux下 ss -i 可显示rto. how to display tcp rto http://linuxaleph.blogspot.com/2013/07/how-to-display-tc ...
- 如何判断单链表是否存在环 & 判断两链表是否相交
给定一个单链表,只给出头指针h: 1.如何判断是否存在环? 2.如何知道环的长度? 3.如何找出环的连接点在哪里? 4.带环链表的长度是多少? 解法: 1.对于问题1,使用追赶的方法,设定两个指针sl ...
- docker中自定ingress网络
在某些时候,docker自动生成的ingress网络会与服务器上已经存在的网络产生冲突,这个时候,你需要自定义ingress. 在自定义前,你需要删除所有有端口发布的服务. 使用命令docker ne ...
- MongoDB插入文档
db.collection.insertOne() 插入单个文档.db.collection.insertMany() 插入多个文档.db.collection.insert() 插入单/多个文档. ...
- linux 里的`反引号
Shell中可以将数字或字符直接赋予变量,也可以将Linux命令的执行结果赋予变量,如下: (1) $ count=9 #将数字赋予变量count (2) $ name=" ...