hibernate的报错信息a different object with th…
2018-11-05 08:22:46来源:博客园 阅读 ()
废话不多说,直接说原因,这是在hibernate中,有2个相同类型的实体类具有同样的主键标识符,然后调用update或者调用saveOrUpdate,我朋友出这个错的由于他想要update一条数据时,获取主键时从数据库查询获取,此时接收的对象的主键id是12,吧这个值赋给要更新入参的对象,2个对象的主键就都是12了,所有会报错,
1 @Override 2 public DTO createExamInfo(DTO dto) { 3 4 Integer examId = GetParamUtil.convertObject(dto.getParam("examId")); 5 ExamInfo examInfo=new ExamInfo(); 6 ExamInfo examInfo = examInfoList.get(0); 7 8 examInfo.setExamId(examId); 9 Integer examPaperId = GetParamUtil.convertObject(dto.getParam("examPaperId")); 10 examInfo.setExamPaperId(examPaperId); 11 String title = GetParamUtil.converObject(dto.getParam("title")); 12 examInfo.setTitle(title); 13 Integer pattern = GetParamUtil.convertObject(dto.getParam("pattern")); 14 examInfo.setPattern(pattern); 15 Integer type = GetParamUtil.convertObject(dto.getParam("type")); 16 examInfo.setType(type); 17 Integer form = GetParamUtil.convertObject(dto.getParam("form")); 18 examInfo.setForm(form); 19 Integer monitor = GetParamUtil.convertObject(dto.getParam("monitor")); 20 examInfo.setMonitor(monitor); 21 String isPerpetual = GetParamUtil.converObject(dto.getParam("isPerpetual")); 22 examInfo.setIsPerpetual(isPerpetual); 23 String startTime = GetParamUtil.converObject(dto.getParam("startTime")); 24 examInfo.setStartTime(startTime); 25 String endTime = GetParamUtil.converObject(dto.getParam("endTime")); 26 examInfo.setEndTime(endTime); 27 String examCreateTime = GetParamUtil.converObject(dto.getParam("examCreateTime")); 28 examInfo.setExamCreateTime(examCreateTime); 29 String createDate = GetParamUtil.converObject(dto.getParam("createDate")); 30 examInfo.setCreateDate(createDate); 31 examInfo.setIsUsed(0); 32 examInfo.setDelFlg("0"); 33 List<ExamInfo> examInfoList=examInfoDao.findByProperty("examId", examId); 34 if (examInfoList!=null&&examInfoList.size()>0) { 35 examInfo.setId(examInfoList.get(0).getId()); 36 examInfoDao.update(examInfo); 37 }else{ 38 examInfoDao.save(examInfo); 39 } 40 41 return null; 42 }
第33行查出了这条数据,此时会话中包含一个id是12的对象(就是此时想要更新的那条数据的主键),然后35行又把id赋值给了另一个对象,执行update时就会报错,我给他改进了一下代码,如下:
1 @Override 2 public DTO createExamInfo(DTO dto) { 3 4 Integer examId = GetParamUtil.convertObject(dto.getParam("examId")); 5 List<ExamInfo> examInfoList=examInfoDao.findByProperty("examId", examId); 6 // ExamInfo examInfo=new ExamInfo(); 7 ExamInfo examInfo = examInfoList.get(0); 8 9 examInfo.setExamId(examId); 10 Integer examPaperId = GetParamUtil.convertObject(dto.getParam("examPaperId")); 11 examInfo.setExamPaperId(examPaperId); 12 String title = GetParamUtil.converObject(dto.getParam("title")); 13 examInfo.setTitle(title); 14 Integer pattern = GetParamUtil.convertObject(dto.getParam("pattern")); 15 examInfo.setPattern(pattern); 16 Integer type = GetParamUtil.convertObject(dto.getParam("type")); 17 examInfo.setType(type); 18 Integer form = GetParamUtil.convertObject(dto.getParam("form")); 19 examInfo.setForm(form); 20 Integer monitor = GetParamUtil.convertObject(dto.getParam("monitor")); 21 examInfo.setMonitor(monitor); 22 String isPerpetual = GetParamUtil.converObject(dto.getParam("isPerpetual")); 23 examInfo.setIsPerpetual(isPerpetual); 24 String startTime = GetParamUtil.converObject(dto.getParam("startTime")); 25 examInfo.setStartTime(startTime); 26 String endTime = GetParamUtil.converObject(dto.getParam("endTime")); 27 examInfo.setEndTime(endTime); 28 String examCreateTime = GetParamUtil.converObject(dto.getParam("examCreateTime")); 29 examInfo.setExamCreateTime(examCreateTime); 30 String createDate = GetParamUtil.converObject(dto.getParam("createDate")); 31 examInfo.setCreateDate(createDate); 32 examInfo.setIsUsed(0); 33 examInfo.setDelFlg("0"); 34 if (examInfoList!=null&&examInfoList.size()>0) { 35 // examInfo.setId(examInfoList.get(0).getId()); 36 examInfoDao.update(examInfo); 37 }else{ 38 examInfoDao.save(examInfo); 39 } 40 41 return null; 42 }
直接采用查出数据时接收的对象作为更新时的入参,在这期间给对象set参数,这样从始至终主键为12的对象就只有一个了
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- Java框架之Hibernate实战篇 2020-06-09
- 【Java-jxl插件】【Excel文件读写报错】jxl.read.biff.BiffE 2020-06-07
- springboot 启动报错"No bean named 'org. 2020-05-29
- 程序员发面试短信,HR十天后才回复信息还被拒:你算什么东西 2020-05-28
- 【报错】导入项目后报错:Target runtime Apache Tomcat v7. 2020-05-25
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash