废话不多说,直接说原因,这是在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. Android学习——BroadCast(一)

    初识广播 BroadCast即为广播,为安卓四大组件之一,用于在应用程序和Activity间传输信息.一条广播,分为发送和接收两部分,发送方通过Intent存储信息,并进行发送.接收方通过BroadC ...

  2. 关于实现XX系统设计时所实现的质量属性战术

    可用性: 1)使用Try-catch对抛出的异常进行处理 2)使用Spring事务管理 易用性: 1)在类似删除相关选项时,弹出提示框,防止误操作 2)在不编辑基本信息时,对其进行折叠或者隐藏 3)提 ...

  3. ACM HDU 1755 -- A Number Puzzle

    A Number Puzzle Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. getResource和getResourceAsStream

    1. 前言 在Java中获取资源的时候,经常用到getResource和getResourceAsStream,本文总结一下这两种获取资源文件的路径差异. 2.Class.getResource(St ...

  5. springMVC文件上传大小超过限制的问题

    [转自]https://my.oschina.net/ironwill/blog/646762 springMVC是一个非常方便的web层框架,我们使用它的文件上传也非常的方便. 我们通过下面的配置来 ...

  6. mxnet导入图像数据

    图像的标签在一个json文件中. %matplotlib inline import json import gluonbook as gb import mxnet as mx from mxnet ...

  7. docker-3-常用命令(下)

    importance 1.启动守护式容器 docker run -d 容器名   使用镜像centos:latest以后台模式启动一个容器 docker run -d centos   问题:然后do ...

  8. springmvc(2)处理器设配器和映射器

     非注解的处理器 映射器 和 适配器 一.处理器映射器 1.BeanNameUrlHandlerMapping <bean class="org.springframework.web ...

  9. 【Javascript-ECMA6-Fetch详解】

    Fetch 由于Fetch API是基于Promise设计,因此旧的浏览器并不支持该API,需要引用时引用es6-promise. 基本知识 fetch请求返回response格式 body Fetc ...

  10. GoBelieve-国内唯一开源IM服务

    GoBelieve-国内唯一开源IM服务 1. 一小时接入 专注IM,无冗余功能 几行代码,一小时接入 省时省力. 2. 自由定制 提供最新源码, 自行二次开发,业务协议 交互视觉均可根据业务需求 自 ...