cienhau wrote:
Hi All,
I have classes called Role and User as below.
When I call user.removeRole(role); the role is removed from the Collection roles in user.
But the rows in the db are not updated, thus when I load user, it will still have the role that I have removed from its roles before.
public class Role ... {
...
Collection users = new TreeSet();
...
...
/**
* ...
*@hibernate.set
* table="USER_ROLE"
* lazy="true"
* cascade="save-update"
* @hibernate.collection-key
* column="ROLE_ID"
* @hibernate.collection-many-to-many
* class="User"
* column="USER_ID"
*/
public Collection getUsers() {
return users;
}
public void setUsers(Collection users) {
this.users = users;
}
...
}
public class User ... {
...
Collection roles = new TreeSet();
...
...
/**
* ...
*@hibernate.set
* table="USER_ROLE"
* inverse="true"
* lazy="true"
* cascade="save-update"
* @hibernate.collection-key
* column="USER_ID"
* @hibernate.collection-many-to-many
* class="Role"
* column="ROLE_ID"
*/
public Collection getRoles() {
return roles;
}
public void setRoles(Collection roles) {
this.roles = roles;
}
....
public void removeRole(Role role) {
role.getUsers().remove(this);
roles.remove(role);
}
}
I am using Hibernate 2.1 and MySQL Server 4.1.
I'd really appreciate if you can give me some advice on this. Thank you.
Best Regards,
Antonius Ng
cascade="save-update" should be cascade="all-delete-orphan"