-->
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.  [ 9 posts ] 
Author Message
 Post subject: How to delete many-to-many association?
PostPosted: Tue Aug 26, 2003 4:10 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 3:34 pm
Posts: 54
Location: Farroupilha - Brasil
Situation:
I have an User and a Group. The users can have some groups, therefore I have an n-n relationship.
Tables:
user, group and user_group.

I am adding a group to the user:

group = new Group(1);
user.getGroups().add(group) # Just add a tuple in user_group. OK
session.update(user);
session.flush();

How to remove it ? I tried this but didn't work:

group = (Group) session.load(Group.class, id);
user.getGroups().remove(group);
session.update(user);
session.flush();

What's the solution?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 26, 2003 4:18 pm 
Newbie

Joined: Tue Aug 26, 2003 9:59 am
Posts: 19
Location: Atlanta, GA
I think you need to set
Code:
cascade=all
or
Code:
cascade=all-delete-orphan
.

_________________
Bill Siggelkow


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 26, 2003 6:25 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2003 4:19 pm
Posts: 42
Try group.getUsers.remove(user);
Also, the update is not needed, flush will save the changes.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 26, 2003 8:42 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 3:34 pm
Posts: 54
Location: Farroupilha - Brasil
Changing to

Code:
cascade="all"

or
Code:
cascade="all-delete-orphan"

or
Code:
group.getUsers().remove(user);


I get

net.sf.hibernate.HibernateException: Another object was associated with this id (the object with the given id was already loaded)

Because the group was "loaded", I think !!

If I instantiate a group without load doesn't work because the comparison doesn't occur only on id. Correct ?

Code:
Group group = new Group(id);
model.getPrivileges().remove(group);  #Does not work


Some idea ???


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 26, 2003 9:14 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
The contract for java.util.Set requires that equals()/hashCode() be implemented by classes that are contained in sets.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 26, 2003 9:33 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 3:34 pm
Posts: 54
Location: Farroupilha - Brasil
I need to implement equals() and hashCode() in the Group class ? Why? To implement a comparison just using the id of the class without load the object?

[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 26, 2003 9:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
You really might want to check out the documentation on the java.util.Set interface. A Set, unlike a list, will not contain duplicate entries as determined by use of the hashCode() method.

When you try to remove a particular object from a Set, the hashCode() of the object object passed to the Set.remove() method is used to remove the appropriate object. Thus if you don't have hasCode() overriden properly, Set.remove() will probably have no effect.

And you need to override equals() because that should be done every time you override hashCode().


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 26, 2003 10:55 pm 
Regular
Regular

Joined: Tue Aug 26, 2003 3:34 pm
Posts: 54
Location: Farroupilha - Brasil
I had sucess implementing equals() and hashCode(). :-)

As I never made this before, I haven't sure if this is the best way to implement it.

Code:
public class Group {
int id;   #is the primary key
.....
public boolean equals(Object o) {
        if (o == this)
            return true;
       
        if (!(o instanceof Group))
       return false;
           
        Group g = (Group) o;
       
        if (g.id == this.id)
            return true;
       
        return false;
    }
   
    public int hashCode() {
        return this.id;
    }


Perhaps exists a better solution ? What you think?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 1:56 pm 
Newbie

Joined: Wed Aug 27, 2003 1:47 pm
Posts: 2
nvolpini wrote:
I had sucess implementing equals() and hashCode(). :-)

As I never made this before, I haven't sure if this is the best way to implement it.

Perhaps exists a better solution ? What you think?


I use Jakarta Commons Lang package

Code:
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
.....
   public boolean equals(Object o) {
       if (!(o instanceof Child)) {
         return false;
       }
      Child rhs = (Child) o;
       return new EqualsBuilder()
                  .append(id, rhs.id)
                  .isEquals();
    }

   public int hashCode() {
     return new HashCodeBuilder(17, 37).   
      append(id).
      toHashCode();
   }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.