tags:

views:

199

answers:

1

Is there a way to add a single criteria on a session factory(configuration) level wich will apply to all entites retreived with a Session ? We have a requirement to not delete any rows from our database but "mark" deleted entities as such so that they will not participate in any further operations.

I know that we can just retrive all entites through a common interface (e.g. a common base Dao object), but the approach with global filtering would be less error-prone as it doesn't require the knowledge of using this common interface.

+2  A: 

The Hibernate Documentation elaborates on this a little more. It looks like the best way to handle this is with Filters.

First, you define a fitler on a class or collection like so:

<filter-def name="IsDeletedFilter">
  <filter-param name="IsDeleted" type="bool"/>
</filter-def>

Then you attach it to a class or collection:

<class/set  ...>
...
<filter name="IsDeletedFilter" condition=":IsDeleted = Is_Deleted"/>

Alas, you must do this with every class you want soft-deleted. But you may be able to map out a base class SoftDeleteEntity and have a table per subclass hierarchy.

I have also seen some people claim to be able to do this with listeners, though I know not how.

Gilligan