当saveorUpdate时,错误“具有相同标识符值的不同对象已与会话关联”

时间:2010-08-16 14:43:41

标签: java hibernate orm

  

可能重复:
  Hibernate: different object with the same identifier value was already associated with the session

使用Hibernate,我面临一个例外a different object with the same identifier value was already associated with the session

我不知道这个例外的原因是什么以及如何处理它?如果有人知道请教我,谢谢。

接下来是我的代码的一部分,

public void addRoleToUser(String userLogin, RoleBean roleToAdd)
  throws FunctionalException, TechnicalException {
    UserBean userBean = readInternal(userLogin);
    userBean.getRoles().add(roleToAdd);
    dao.save(userBean);  //dao is an instance of DAOCLASS
}

在“dao.save”功能期间

try {
   session = sessionFactory.openSession();
   log.debug("session="+session.hashCode()+" save "+e);
   session.saveOrUpdate(e); //this line throws an exception
   session.flush();
}
catch (Exception e1) {
    //e1 now is "a different object with the same identifier value was already associated with the session"
} 

0 个答案:

没有答案