-->
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.  [ 8 posts ] 
Author Message
 Post subject: Many-to-Many junction table not being updated
PostPosted: Tue Mar 09, 2004 5:43 am 
Beginner
Beginner

Joined: Wed Mar 03, 2004 6:02 am
Posts: 46
Location: Kuala Lumpur, Malaysia
Using: Hibernate 2.1.2, MySQL 4

Code:
[net.sf.hibernate.engine.Cascades] processing cascades for: X.model.User
[net.sf.hibernate.engine.Cascades] cascading to collection: X.model.User.roles
[net.sf.hibernate.engine.Cascades] cascading to saveOrUpdate()
[net.sf.hibernate.engine.Cascades] version unsaved-value strategy UNDEFINED
[net.sf.hibernate.engine.Cascades] id unsaved-value strategy NULL


I have a many-to-many relationship between User and Role.
I'm trying to remove a Role from a User but it's not doing anything to the junction table.
Does the error messages above hold any significance with regards to my problem ?

My mapping document:

Code:
    <class
        name="X.model.User" table="user" ...>
       <set
            name="roles"
            table="user_role"
            lazy="false"
            inverse="true"
            cascade="all"
            sort="unsorted"
        >
              <key column="user_id"/>
              <many-to-many
                  class="X.model.Role"
                  column="role_id"
                  outer-join="auto"
              />
        </set>   
        ---o<--- snipped for brevity -----
    </class>

    <class
        name="X.model.Role" table="role" ...>
        <set
            name="users"
            table="user_role"
            lazy="false"
            inverse="false"
            cascade="all"
            sort="unsorted"
        >
              <key
                  column="role_id"   />

              <many-to-many
                  class="X.model.User"
                  column="user_id"
                  outer-join="auto"
              />
        </set>
        ---o<--- snipped for brevity -----
    </class>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 6:07 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Are you removing the associations from both collections?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 09, 2004 11:11 pm 
Beginner
Beginner

Joined: Wed Mar 03, 2004 6:02 am
Posts: 46
Location: Kuala Lumpur, Malaysia
OK. I am removing the association from both side.

i.e. User.removeARole(Role r) does two things:

a) Role.removeAUser(this)
b) users.remove( r )

I found out that line (a) fails.

More questions:
- Does the order matter (a before b, b before a) ?
- I can't fathom why line (a) fails because the hashCode() of the User instance matches the one in member Role.users ? Does CGLIB do anything strange to the equals method ? Note that outside of Hibernate, I can safely add a User into a HashSet and remove it.

Regards,

Alistair


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 5:04 am 
Beginner
Beginner

Joined: Wed Mar 03, 2004 6:02 am
Posts: 46
Location: Kuala Lumpur, Malaysia
OK. I'm able to remove from both collections now BUT to make this work; I had to *NOT* override equals/hashCode and use the default reference equality.

However, in my session bean method, it does not update the Database in any way.
Code:
session.saveOrUpdate( theUser );

Some extracts of the log:
Code:
Processing unreferenced collections
Scheduling collection removes/(re)creates/updates
Flushed: 0 insertions, 2 updates, 0 deletions to 2 objects
Flushed: 0 (re)creations, 1 updates, 0 removals to 2 collections


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 10:27 am 
Beginner
Beginner

Joined: Wed Mar 03, 2004 6:02 am
Posts: 46
Location: Kuala Lumpur, Malaysia
OK. The only time I could get rows deleted from the junction table, user_role is by setting the cascade option for class User to "all-delete-orphan". Unfortunately it also deleted the record in Role's table. But I want Role's lifecycle to be independent of Users. Also can anyone offer advice on why I cannot override the hash/equals of my class & especially why relying on the default reference equality in Object is working for me in this case ?

Regards,

Alistair


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 5:14 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Try to double check your equals/hashCode implementation, there should be something wrong

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 10, 2004 10:54 pm 
Beginner
Beginner

Joined: Wed Mar 03, 2004 6:02 am
Posts: 46
Location: Kuala Lumpur, Malaysia
Merci mille fois Emmanuel!

Voila mes hashCode() and equals():-

Code:
public boolean equals(Object obj) {

  if (this == obj)
    return true;

  if((obj == null) || (obj.getClass() != this.getClass()))
    return false;

  User other = (User) obj;
    return new EqualsBuilder()
     .append(getUserName(), other.getUserName())
     .isEquals();
}

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


Mes meilleures amiti


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 12, 2004 2:00 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
usually instanceof is used.
Does this help http://www.hibernate.org/109.html

_________________
Emmanuel


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