If I delete an entity and then flush the session, if the entity is referred to anywhere else in the session cache, I get
ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations)
The code that's doing the delete doesn't know anything about the specific type of the entity being deleted, and there can be a lot of stuff in the session cache, so it would be major pain to somehow search through the session cache for other entities that refer to the one being deleted and clear the references (using Reflection?). Is it even possible to walk through the session cache like that?
I guess I could resort to requiring every entity to implement an interface defining a write-once "IsDeleted" property and a "Deleted" event. All entities with properties referencing other entities would have to subscribe/unsubscribe to the event when a referenced instance is associated/dissociated. An implementation of IInterceptor.OnDelete() could then set the IsDeleted property on the entity being deleted (if it implements the interface), and the deleted entity would then raise its event. Then the referring entities, listening to the event, could clear their references to the deleted object, freeing the deleted entity or any other code from that responsibility. Still sounds like a hassle though.
Does anyone else have this problem? Is it because my sessions live too long and have too much stuff in them (i.e. the "solution" is to always have sessions so short-lived and sparse that any code using them should know exactly what's in the cache?)