hibernate的报错信息a different object with the same identifier value was already associated with the session解决办法
程序员文章站
2023-02-09 22:17:24
废话不多说,直接说原因,这是在hibernate中,有2个相同类型的实体类具有同样的主键标识符,然后调用update或者调用saveOrUpdate,我朋友出这个错的由于他想要update一条数据时,获取主键时从数据库查询获取,此时接收的对象的主键id是12,吧这个值赋给要更新入参的对象,2个对象的 ......
废话不多说,直接说原因,这是在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的对象就只有一个了
上一篇: 小学生现代诗歌
下一篇: 失眠了肿么破 安神养生粥拿走不谢