hibernate的报错信息a different object with th…

2018-11-05 08:22:46来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

废话不多说,直接说原因,这是在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
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:获取数据库连接对象Connection

下一篇:Java 多线程(四)—— 单例模式