-->
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.  [ 3 posts ] 
Author Message
 Post subject: Removing many-to-many associations
PostPosted: Wed Jun 29, 2005 9:33 pm 
Newbie

Joined: Thu Jun 23, 2005 2:08 am
Posts: 1
Hi,

Im using a many-to-many association to map Users to Roles. I'm trying to remove an association, lets say, unjoin a user from a role. Basically im expecting this to just remove a record from the association table.

I have code like this:

Code:
Role role1 = new Role( "role1" );
Role role2 = new Role( "role2" );

Save(role1);  // Call session.Save()
Save(role1);  // Call session.Save()

User user = new User( "user1" );
user.Roles.Add( role1 );
role1.Users.Add( user );

user.Roles.Add( role2 );
role2.Users.Add( user );

Save( user );

// All is good up to this point

user.Roles.Remove( role1 );
role1.Users.Remove( user );

Update( user ); // Call session.Update()


On the call to session.Update() I get a HibernateException with the message : another object with the same id was already associated with the session

I don't know if im missing something, doing something wrong or is this a known issue. Please help. Thanks!

Here's part of my xml mapping file

Code:
<subclass
   name="Project.Security.Roles.Role, Project.Security"
   discriminator-value="Role">
      
   <bag
      name="Users"
      table="UserRole"
      lazy="false"
      cascade="save-update"
      inverse="true" >
      
      <key column="RoleID" />
      <many-to-many
         column="UserID"
         class="Project.Security.Roles.User, Project.Security" />
   </bag>
   
</subclass>

<subclass
   name="Project.Security.Roles.User, Project.Security"
   discriminator-value="User" >

   <bag
      name="Roles"
      table="UserRole"
      lazy="false"
      cascade="save-update" >
      
      <key column="UserID" />
      <many-to-many
         column="RoleID"
         class="Project.Security.Roles.Role, Project.Security" />
   </bag>

</subclass>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 30, 2005 10:49 am 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
First, I'm assuming that the repeated Save(role1) was just a transcription error. If not, could that be the problem?

Since the User object is already associated with the session, have you tried just calling Flush()? Also, what happens if you close the "saving" session, then open a new session for the update?

Another thought: since the user is the object that is controlling the association (inverse='false') then you shouldn't have to remove the user from role.Users to accomplish the disassociation in the database (though you'd still need to handle it locally I guess).

Final thought: is there a requirement in your system that each Role contain a list of subscribing Users? It would greatly simplify and streamline your system if you left that to a "query only" situation, or at least made that collection lazy. Consider: because your collections aren't lazy, every time you load a User you not only get the Roles associated with it, but also every single User associated with that Role in turn, until the graph is complete. With a comprehensive Role system, that could lead to the loading of almost every User in your system!

I think. Anyone feel free to correct me if I am wrong.

I think.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 01, 2005 12:42 am 
Hi marcal,

I've tried both using a new session and reusing the old session, but results are still the same.

With regard to the requirements, there is none, I was just trying out nhibernate's capabilities. I guess I'll have to use another approach then, just use a one-to-many maybe.

But has this been an issue to anyone else? If so, I'll try to post it in jira with tests and all.

Cheers!


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