Using Hibernate, I'm facing an exception saying a different object with the same identifier value was already associated with the session
.
I don't know what's the cause of this exception and how to deal with it? If someone knows please teach me, thanks.
And next is a part of my code,
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
}
during the function of "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"
}