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?