-->
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.  [ 11 posts ] 
Author Message
 Post subject: all-delete-orphan
PostPosted: Tue Sep 30, 2003 11:12 am 
Beginner
Beginner

Joined: Tue Sep 30, 2003 10:09 am
Posts: 34
Location: London, UK
I'm trying to take advantage of cascade="all-delete-orphan" to automatically delete persistent child objects from the database when they have lost their association with their parent.

I have a one-to-many mapping from Role to Permission, and a many-to-one in the opposite direction. The parent Collection on Role is a Map, with lazy="false" and inverse="true" and cascade="all-delete-orphan".

When a new Permission is added to the Role, I'm setting it's parent role and putting it into the Map on the Role.

So, as far as I can tell, everything is set up correctly; creation of new Permissions works fine. However, removing a Permission from the Map on Role does not remove the permission row from the database.

I've also tried setting the role field to null, to ensure the association is removed from the child end, but this made no difference.

It's maybe worth mentioning that a new session is being used for each distinct load or modify operation on my DAOs.

From the log debug I can see that the new Permission is correctly loaded:

Quote:
16:01:04,758 DEBUG StringType:65 - returning 'ff808081f7f3041a00f7f304418e0001' as column: id
16:01:04,759 DEBUG Loader:260 - result row: ff808081f7f3041a00f7f304418e0001
16:01:04,760 DEBUG Loader:357 - Initializing object from ResultSet: ff808081f7f3041a00f7f304418e0001
16:01:04,761 DEBUG Loader:413 - Hydrating entity: foo.domain.Permission#ff808081f7f3041a00f7f304418e0001
16:01:04,762 DEBUG IntegerType:65 - returning '14' as column: resource2_
16:01:04,763 DEBUG IntegerType:65 - returning '3' as column: operatio3_
16:01:04,764 DEBUG StringType:65 - returning '301' as column: roleid
16:01:04,765 DEBUG StringType:65 - returning 'ff808081f7f3041a00f7f304418e0001' as column: id__
16:01:04,766 DEBUG SessionImpl:1752 - loading [foo.domain.Permission#ff808081f7f3041a00f7f304418e0001]
16:01:04,769 DEBUG SessionImpl:1843 - attempting to resolve [foo.domain.Permission#ff808081f7f3041a00f7f304418e0001]
16:01:04,770 DEBUG SessionImpl:1858 - resolved object in session cache [foo.domain.Permission#ff808081f7f3041a00f7f304418e0001]
16:01:04,771 DEBUG StringType:65 - returning 'ff808081f7f3041a00f7f304418e0001' as column: id__
16:01:04,772 DEBUG Loader:182 - done processing result set (1 rows)
16:01:04,773 DEBUG BatcherImpl:173 - done closing: 0 open PreparedStatements, 0 open ResultSets
16:01:04,774 DEBUG SessionFactoryImpl:554 - closing statement
16:01:04,774 DEBUG Loader:195 - total objects hydrated: 1
16:01:04,775 DEBUG SessionImpl:1954 - resolving associations for [foo.domain.Permission#ff808081f7f3041a00f7f304418e0001]
16:01:04,776 DEBUG SessionImpl:1752 - loading [foo.domain.Role#301]
16:01:04,778 DEBUG SessionImpl:1843 - attempting to resolve [foo.domain.Role#301]
16:01:04,779 DEBUG SessionImpl:1858 - resolved object in session cache [foo.domain.Role#301]
16:01:04,780 DEBUG SessionImpl:1975 - done materializing entity [foo.domain.Permission#ff808081f7f3041a00f7f304418e0001]
16:01:04,781 DEBUG SessionImpl:1975 - done materializing entity [foo.domain.Role#301]
16:01:04,782 DEBUG SessionImpl:435 - closing session
16:01:04,783 DEBUG SessionImpl:2930 - disconnecting session
16:01:04,783 DEBUG SessionImpl:447 - transaction completion


Then after the delete of the Permission, my own log output shows the Role's Map no longer contains the Permission I deleted, but the Hibernate log shows no corresponding delete occurring:
Quote:
16:01:04,809 DEBUG Cascades:336 - processing cascades for: foo.domain.Role
16:01:04,810 DEBUG Cascades:275 - cascading to collection: foo.domain.Role.permissions
16:01:04,811 DEBUG Cascades:344 - done processing cascades for: foo.domain.Role
16:01:04,811 DEBUG SessionImpl:268 - Collection dirty: [foo.domain.Role.permissions#301]
16:01:04,812 DEBUG SessionImpl:2113 - Flushing entities and processing referenced collections
16:01:04,816 DEBUG SessionImpl:2209 - Updating entity: [foo.domain.Role#301]
16:01:04,817 DEBUG SessionImpl:2550 - Collection found: [foo.domain.Role.permissions#301], was: [foo.domain.Role.permissions#301]
16:01:04,818 DEBUG SessionImpl:2397 - Processing unreferenced collections
16:01:04,818 DEBUG SessionImpl:2408 - Scheduling collection removes/(re)creates/updates
16:01:04,819 DEBUG SessionImpl:2023 - Flushed: 0 insertions, 1 updates, 0 deletions to 1 objects
16:01:04,819 DEBUG SessionImpl:2028 - Flushed: 0 (re)creations, 1 updates, 0 removals to 1 collections
16:01:04,820 DEBUG SessionImpl:2058 - executing flush


It looks like it's doing an update on the Permission, and not a delete.

What am I doing wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 30, 2003 11:15 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I'm not sure. Not enough information to help any further....


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 30, 2003 11:31 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
Post a little of the code that handles the remove, as well as those bits of the mapping files. I think they'll be able to help you a little better then.

All I can suggest, before that, is make sure you are correctly breaking the link between the object and the collection.

-G


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 30, 2003 11:51 am 
Beginner
Beginner

Joined: Tue Sep 30, 2003 10:09 am
Posts: 34
Location: London, UK
OK, I'm using 2.0.3. Here are the Role and Permission definitions:

Code:
      <class name="foo.domain.Role" table="role">
       
         <id name="id" column="id" type="string" unsaved-value="null">
            <generator class="uuid.hex"/>
         </id>
         <property name="groupId" column="groupid" type="string" not-null="true" update="true" />
         <property name="roleNameId" column="rolenameid" type="string" not-null="true" update="true" />
               
         <map name="permissions" table="permission" lazy="false" order-by="resourcetypeid asc" inverse="true" cascade="all-delete-orphan">
            <key column="roleid" />                  <!-- The foreign key field -->
            <index column="id" type="string" />      <!-- The key for the hash map -->
            <one-to-many class="foo.domain.Permission" />
         </map>

      </class>



Code:
      <class name="foo.domain.Permission" table="cks_permission">
       
         <id name="id" column="id" type="string" unsaved-value="null">
            <generator class="uuid.hex"/>
         </id>
         <property name="resourceTypeId" column="resourcetypeid" type="int" not-null="true" update="true" />
         <property name="operationId" column="operationid" type="int" not-null="true" update="true" />
         
         <many-to-one name="role" column="roleid" class="foo.domain.Role" not-null="true" />
               
      </class>


The add code from Role:

Code:
   public Permission addPermission(ResourceType type, Operation operation)
   {
      Permission result = new Permission(this, type, operation);
      // Add to the map of all Permissions
      m_permissions.put(result.getKey().getId(), result);
      return result;
   }


The corresponding delete method from Role:

Code:
   public void deletePermission(Permission target)
   {
      target.setRole(null);
      m_permissions.remove(target.getKey().getId());
   }


The Permission constructor:

Code:
   public Permission(Role role, ResourceType type, Operation operation)
   {
      m_role = role;
      m_type = type;
      m_operation = operation;

      this.setKey(new PermissionKey());
   }



Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 30, 2003 11:53 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Eh?? Looks like you are putting ids in the collection instead of the actual permissions!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 30, 2003 11:59 am 
Beginner
Beginner

Joined: Tue Sep 30, 2003 10:09 am
Posts: 34
Location: London, UK
Well, the Map is keyed by ID of the permission, but the entry value is the Permission. This OK?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 30, 2003 12:08 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Oh, sorry, I misread.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 30, 2003 12:13 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Well, I'm not sure if I have time to look at this now.

Try putting a break point in n.s.h.collection.Map.getOrphans() and see if we properly detect the removed orphans.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 30, 2003 12:42 pm 
Beginner
Beginner

Joined: Tue Sep 30, 2003 10:09 am
Posts: 34
Location: London, UK
OK, will take a look tomorrow. Thanks Gavin.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 01, 2003 8:59 am 
Beginner
Beginner

Joined: Tue Sep 30, 2003 10:09 am
Posts: 34
Location: London, UK
OK, as I expected (no, really!) the problem was in my code. Well, not in my code, but in my unit test.

It was using the Role with the new, unpersisted Permissions on it, and not the reloaded Role, with the persisted Permissions on it. Hence the Permission was indeed removed from the Role, but it was the wrong Role...

Thanks for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 01, 2003 9:04 am 
Beginner
Beginner

Joined: Tue Sep 30, 2003 10:09 am
Posts: 34
Location: London, UK
I should add that it all works beautifully :)


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