日期:2014-05-18  浏览次数:20629 次

hibernate 更新数据的时候需要在同一个session中更新2条记录
我的项目中现在用到了openSessionInViewFilter,但是现在我的action里面的updateCategory需要同时更新一张表里面的2条记录,结果出现了a different object with the same identifier value was already associated with the session 这样的异常,应该怎么解决呢?

Category parCategory=categoryManager.getCategoryById(new Integer(parId));
Category oriParCategory=categoryManager.getCategoryById(category.getCategory_id()).getParCategory();
if(oriParCategory!=null) {
  if(oriParCategory.getChildCategorySet().size()<=1) {
  oriParCategory.setHasSub(0);
categoryManager.updateCategory(oriParCategory);
  }
}
categoryManager.updateCategory(parCategory);

------解决方案--------------------
这个异常是说,不同的对象有相同的标识码,也就是说主键冲突了
可以这样解决这个问题,修改语句时不用update()方法,而是用merge()方法就可以了;