Can anyone recommend a good way to ensure that database rows representing "derefenced objects" are cleaned up? That is, how should I implement the equivalent of garbage-collection for my database?
Transitive persistence is truly handy, but as the Hibernate manual says:
Quote:
A child which becomes unreferenced by its parent is not automatically deleted, except in the case of a <one-to-many> association mapped with cascade="delete-orphan".
I have associations in my domain-model that don't naturally map to one-to-many, e.g.
- one-to-{zero,one} associations, where a child object may be null, or replaced
- many-to-many parent/child relationships where a child may be shared between "parents", but should be removed once it's no longer referenced
I accept that Hibernate can't help me in these situations. I imagine that calling session.delete() explicitly would work, but I don't want to tie my domain-model to Hibernate, and still want it to work sensibly even when not persisted.
So, how should I clean up data in these cases? What has worked for you?