废话不多说,直接说原因,这是在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解决办法的更多相关文章

  1. 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"?> ...

  2. 关于报错“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 ...

  3. Centos7最小化安装报错There are no enabled repos. Run "yum repolist all" to see the repos you have.解决办法

    原因是缺少CentOS-Base.repo文件,因为我这台机器wget也不能用,所以我是下载到本地sftp上去的,传输的时候一定要在root用户下,否则会无法启动传输 这是报错的完整信息:Loadin ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. angular 升级到angular8 以及报错信息解决

    1.升级全局angular-cli npm install -g @angular/cli@latest 2.升级项目内 angular-cli (在需要升级的项目中运行) npm i @angula ...

  8. 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 ...

  9. Python 装饰器填坑指南 | 最常见的报错信息、原因和解决方案

    本文为霍格沃兹测试学院学员学习笔记. Python 装饰器简介 装饰器(Decorator)是 Python 非常实用的一个语法糖功能.装饰器本质是一种返回值也是函数的函数,可以称之为“函数的函数”. ...

随机推荐

  1. 01_NIO基本概念

    [NIO的几个概念] Buffer(缓冲区) Channel(通道,管道) Selector(选择器,多路复用器) [Buffer] Buffer是一个对象,它包括一些要写入或者要读取的数据.在NIO ...

  2. AdobeCS3DesignPremiumChs_Greendown.cn使用注册机激活失败重新激活操作方法

    解决Adobe Acrobat 9 Pro序列号无效/重新激活方法 1.删除C:\Program Files\Common Files\Adobe\Adobe PCD\cache\cache.db和C ...

  3. CRM系统知识点之一权限(RBAC)

    一个项目可以有多个应用 一个做成组件 一个做逻辑判断一个应用(做成组件形式)可以服务于多个项目 rbac权限(role-base access control)who what how什么样的角色对什 ...

  4. springlog记录

    在servlet.xml加入 <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-aut ...

  5. linux 软连接和硬链接示意图

    创建软连接 ln -s 1.txt 1-softlink.txt 创建硬链接 ln 1.txt 1-hardlink.txt

  6. SQL删除多列语句的写法

    SQL删除表中多个列的写法: ALTER TABLE table_name DROP COLUMN COLUMN1,COLUMN COLUMN2,COLUMN COLUMN3 ……

  7. 【NLP汉语自然语言处理与实践】分词_笔记

    一.两种分词标准: 1. 粗粒度. 将词作为最小基本单位.比如:浙江大学. 主要用于自然语言处理的各种应用. 2. 细粒度. 不仅对词汇继续切分,也对词汇内部的语素进行切分.比如:浙江/大学. 主要用 ...

  8. zimbra邮件服务器的搭建和迁移

    背景: 公司最近由于服务器费用问题,需要将邮件服务器从亚马逊(新加坡)云服务器A迁移到阿里云(香港)云服务器B. 由于邮箱使用的是域名访问,但是没有进行备案,所以只能迁移到港澳台地区,才能正常使用. ...

  9. sql 嵌套查询

    sql 某一字段 数量大于1 SELECT * FROM ecm_goods_spec AWHERE ( SELECT count( * ) FROM ecm_goods_spec BWHERE A. ...

  10. LVM逻辑卷的管理和使用

    本篇将从头到尾演示一遍逻辑卷的管理. 主要步骤 1.创建lv逻辑卷步骤     前提:先创建3个磁盘分区,类型为8e:         1.PV创建             pvcrete /dev/ ...