首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

hibernate 更新数据的时候需要在同一个session中更新2条记录解决思路

2012-01-21 
hibernate 更新数据的时候需要在同一个session中更新2条记录我的项目中现在用到了openSessionInViewFilter

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()方法就可以了;

热点排行