Hi,
I have a User, UserRole and Role table. In my application, when i add a user, i must do so with a role. I have the following method on my dao class that does this:
public void addUser(User user, Role role) throws DALException { Transaction transaction = _session.beginTransaction(); try { UserRole userRole = new UserRole(); userRole.setPerson(user); userRole.setRole(role);
Set<UserRole> userRoles = new HashSet<UserRole>(); userRoles.add(userRole); user.setUserRoles(userRoles); _session.save(user);
transaction.commit(); } catch(Exception e) { transaction.rollback(); throw new DAOException(e); } } }
The problem is, the User is added to the User table, but the UserRole table is blank. Can anyone explain to me why this does not work? Do you have to add each object individually or is hibernate smart enough to know that there are related records that need to be inserted as well?
Thanks
|