-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Removing an element from a Collection
PostPosted: Thu May 26, 2005 6:53 am 
Newbie

Joined: Thu May 26, 2005 6:24 am
Posts: 6
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


Top
 Profile  
 
 Post subject: Re: Removing an element from a Collection
PostPosted: Thu May 26, 2005 8:50 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
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"


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 26, 2005 10:39 pm 
Newbie

Joined: Thu May 26, 2005 6:24 am
Posts: 6
Hi Pksiv,

Thanks for the reply.

I just want to deassociate the role from the user (removing it from the Collection attribute that user has), not deleting the role itself.
all-delete-orphan deletes the role..


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 27, 2005 1:06 am 
Newbie

Joined: Thu May 26, 2005 6:24 am
Posts: 6
Hi again,

This is what I have found out:

If I remove the role when the user object is transient, and after that I persist the user object and save it (session.saveOrUpdate(user)), the role will not be removed from the association table of USER_ROLE.

If I remove the role when the user object is persisted, the role is removed from the assocation table.

Strange thing is that if I add a role to the user when the user object is transient, and after that I persist the user object and save it, the new role will be added to the collection. This is not the case when I try to remove the role from the user....

So for the time being, I will persist the user object when removing a role from the user.

Thanks,
Antonius Ng


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.