Hibernate version:
3.1
Name and version of the database you are using:
Oracle 10g
Specifically, making the distinction between "ownership" and "reference". In our model, a User object may "own" many contacts but a contact also has a "reference" to another User which is the target of the contact. When the owning User gets deleted, all of its owned contacts should get deleted. However when a User gets deleted, any contact that reference that User as as target should not get deleted. It's reference should just be set to NULL.
In this specific case(User-Contact), it is possible to just query for all contacts whose target is the to-be-deleted user and then do user.setTarget(null). This works fine, but my question is more generic. Is there any way to make hibernate update all those foreign keys to the User to NULL when the User gets deleted without doing this explicit object.setTarget(null). Going forward, we may have these non-ownership references peppered throughout the model and writing explicit code to clean them up will get very tedious.
The only solutions that I have seen to this so far is to use "ON DELETE SET NULL" on the foreign key, but this seems like it would have at least 2 problems:
1. Your in-memory transient objects now have bad data after the commit because hibernate doesn't know that the database just nulled out a lot of references.
2. Potential issues with the 2nd-level cache for the same reason. This would be more of a problem with us.
l
Any and all comments would be greatly appreciated.
Thanks.
|