hibernate的报错信息a different object with the same identifier value was already associated with the session解决办法

废话不多说,直接说原因,这是在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的对象就只有一个了

posted @ 2018-11-05 11:24  CN-无忧  阅读(693)  评论(0编辑  收藏  举报