读书人

Hibernate读书笔记-a different objec

发布时间: 2012-11-04 10:42:41 作者: rapoo

Hibernate读书笔记-----a different object with the same

在web controller新增加了一个po状态的校验,即将系统中该记录的一个状态值先取出比较web上已经有的状态判断是否发生过变化。exception出现在update这个record的时候。系统报a different object with the same identifier value was already associated。

@RequestMapping(params = "_p_action=save")public ModelAndView save(HttpServletRequest req, HttpServletResponse res,ItemCostNpriceChg itemCostNpriceChg) throws Exception {ModelAndView modelAndView = new ModelAndView("jsonView");Set<ItemCostNpriceDtl> itemCostNpriceDtls = new HashSet<ItemCostNpriceDtl>();HashMap<String, Object> ret = new HashMap<String, Object>();Credential user = userManager.getCredential();try {if (itemCostNpriceChg.getAppNo() == null) {itemCostNpriceChg = (ItemCostNpriceChg) CommUtils.copy(itemCostNpriceChg);itemCostNpriceChg.setBuyer(user.getUserID());itemCostNpriceChg = itemPriceAdjustService.add(itemCostNpriceChg);itemCostNpriceDtls = buildChangeDetail(req, itemCostNpriceChg,"add");itemPriceAdjustService.saveItemCostNpriceDtls(itemCostNpriceDtls);ret = buildWebReturn(itemCostNpriceChg);} else {if (!user.getUserID().equalsIgnoreCase(itemCostNpriceChg.getCreatedBy()))throw new BusinessException("You have no right to update the application that other created!","appNo");if (itemCostNpriceChg.getAppStatus() != 0&& itemCostNpriceChg.getAppStatus() != 4)throw new BusinessException("You can not save the record that was completed!","appNo");ItemCostNpriceChg check=new ItemCostNpriceChg();check=itemPriceAdjustService.getById(itemCostNpriceChg.getAppNo());if(check.getAppStatus().compareTo(itemCostNpriceChg.getAppStatus())!=0)throw new BusinessException("The statuc of the application has been changed ! pls reload the application.","btnNew");check = (ItemCostNpriceChg) CommUtils.copy(itemCostNpriceChg);itemCostNpriceChg=null; //boolean check=itemPriceAdjustService.checkAppStatus(itemCostNpriceChg.getAppNo(),itemCostNpriceChg.getAppStatus());//if(!check)//{//throw new BusinessException(//"The statuc of the application has been changed ! pls reload the application.",//"btnNew");//}//itemCostNpriceChg.setBuyer(user.getUserID());//itemCostNpriceChg.setCreatedBy(user.getUserID());//itemCostNpriceChg.setCreatedTime(DateUtil.getServerDate());//itemPriceAdjustService.update(itemCostNpriceChg);//itemCostNpriceDtls = buildChangeDetail(req, itemCostNpriceChg,//"update");//itemPriceAdjustService//.saveItemCostNpriceDtls(itemCostNpriceDtls);check.setBuyer(user.getUserID());check.setCreatedBy(user.getUserID());check.setCreatedTime(DateUtil.getServerDate());itemPriceAdjustService.update(check);itemCostNpriceDtls = buildChangeDetail(req, check,"update");itemPriceAdjustService.saveItemCostNpriceDtls(itemCostNpriceDtls);ret = buildWebReturn(check);}modelAndView.addObject("data", ret);modelAndView.addObject("success", true);logger.debug(modelAndView.toString());} catch (BusinessException e) {e.printStackTrace();logger.error(e);modelAndView.addObject("success", false);modelAndView.addObject("message", e.getMessage());modelAndView.addObject("target", e.getTarget());} catch (Exception e) {e.printStackTrace();logger.error(e);modelAndView.addObject("success", false);modelAndView.addObject("message", e.getMessage());}return modelAndView;}

?注释部分就是出错代码。分析问题是session中的实体与系统又取出的实体都是在session中,但update时候就会报错。因此有二种解决方法:

1.将session中的

public ModelAndView save(HttpServletRequest req, HttpServletResponse res,ItemCostNpriceChg itemCostNpriceChg) 
itemCostNpriceChg去除,对数据库中po进行赋值操作。代码会多很多,不采用2.将使用merge方法,merge和update的区别是,hibernate会将用户要求的pocopy到session中的po中,因此不会引发冲突

读书人网 >软件架构设计

热点推荐