I have a very similar business case with a slightly different problem.
To illustrate, I have to two objects:
1/ User
2/ Group
The association is as follows:
User -> GroupMembership <- Group
Note: There is not GroupMembership object, this is just the name of the association.
Specifically, a user has a set of Group objects that compose the group memberships for that user. A group has a set of User objects that are members of that group. This is modeled as a bi-directional many-to-many.
If I have a group with a very large membership, then deletion performance can be significantly degraded. This is due to the requirement that I manually dereference the group being deleted from set of group memberships for each user.
I have found that if I do not do this, if a user that belongs to the group being deleted is loaded in the same session and has her group membership set initialised, then exceptions are thrown when the session flushes during the delete operation.
The exception indicates that an attempt was made to save an object during cascade that has already been deleted.
My major issue with this is that there is no easy way to determine which (if any) users have been loaded in the current session and hence must have their memory state updated. I do not wish to iterate the set of members for the group being deleted as it could be enormous and would result in a large amount of data being retrieved unnecessarily from the database.
I understand that this is something that cannot be overcome in hibernate, however I am hoping that there are strategies that might help me to deal with this issue.
Hopefully there is a design pattern that can be of use here. I am unwilling to use hand coded SQL here as I have similar issues in other parts of my data model.
|