User and Role has a bidirectional many-to-many relationship,
both sides are mapped as cascade="save-update",one side is mapped as inverse="true",the other as inverse="false"
How should I delete a Role(or User) object,
Code:
Role role=(Role)session.load(Role.class,roleId);
session.delete(role);
OR
Code:
Role role=(Role)session.load(Role.class,roleId);
for(Iterator iter=role.getUsers().iterator();iter.hasNext();){
((User)iter.next()).getRoles().remove(role);
}
session.delete(role);
I think the second is the right one,but it is very prolix,and may be inefficient.