插入數據時,實體類的克隆


/**
* @param expertsId
* @param localExpertGroup
* @return 分配現場專家
*/
@RequestMapping(method = RequestMethod.POST, value = {“/distribute”})
public ModelAndView distribute(Integer[] expertsId, LocalExpertGroup localExpertGroup) {
ModelAndView mav = new ModelAndView(“jsonView”);
try {
for (int expert : expertsId) {
localInvestigateAdviceService.addDistribute(expert, localExpertGroup);// 原來的代碼想法:前台傳的是數組ID,后台遍歷數組來依次在數據庫中增加一條數據
//出現的問題:比如前台有3個ID傳過來,debug走時也不報錯,但最終數據庫只增加最后一個ID的數據,把前兩條依次覆蓋了
}
mav.addObject(“success”, Boolean.valueOf(true));
} catch (Exception e) {
LOG.error(e.getMessage(), e);
mav.addObject(“msg”, e.getMessage());
mav.addObject(“success”, Boolean.valueOf(false));
}
return mav;
}
//解決的方法:
/**
* @param expertsId
* @param localExpertGroup
* @return 分配現場專家
*/
@RequestMapping(method = RequestMethod.POST, value = {“/distribute”})
public ModelAndView distribute(Integer[] expertsId, LocalExpertGroup localExpertGroup) {
ModelAndView mav = new ModelAndView(“jsonView”);
try {
for (int expert : expertsId) {
localInvestigateAdviceService.addDistribute(expert, (LocalExpertGroup) localExpertGroup.clone());//這行是修改后的代碼
}
mav.addObject(“success”, Boolean.valueOf(true));
} catch (Exception e) {
LOG.error(e.getMessage(), e);
mav.addObject(“msg”, e.getMessage());
mav.addObject(“success”, Boolean.valueOf(false));
}
return mav;
}
//具體如下:
public class LocalExpertGroup extends BaseDomain implements Cloneable//讓這個實體類實現Cloneable這個接口
//並且實體類重寫這個方法:
@Override
public Object clone(){
LocalExpertGroup stc = null;
try{
stc = (LocalExpertGroup) super.clone();
}catch(CloneNotSupportedException e) {
e.printStackTrace();
}
return stc;
}

附:網上的方法:
@Override
public Object clone(){
SiteColumn stc = null;
try{
stc = (SiteColumn) super.clone();
}catch(CloneNotSupportedException e) {
e.printStackTrace();
}
return stc;
}


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com