Search code examples
soft-deleteopenxava

How do I implement soft delete in OpenXava?


OpenXava does not seem to offer any bundled implementation of the soft delete strategy, e.g. a strategy of using a deletedAt column that can be NULL, and setting it to the time the row was deleted, without actually deleting it, or some other similar solutions.

This strategy is commonly used, it is implemented and bundled with other famous ORM frameworks (Sequelize to name one) and it has many advantages in enterprise applications. It does have a few drawbacks, so it's a matter of choice: sometimes you want to keep deleted rows around and just filter them out in most subsequent read operations.

So, how can I implement such strategy in OpenXava?


Solution

  • I've posted this question and answer because searching for openxava soft delete on Google doesn't point you to the relevant documentation straight away (but only as 7th or 8th result), since the official doc doesn't contain the "soft" word anywhere, as of today, despite soft delete being the common name for this strategy.

    Moreover the page title of the documentation that explains the process (namely "Lesson 24: Refining the standard behavior") is way too generic to attract any attention of a developer looking specifically for the soft delete functionality, so the risk is to skip that 7th or 8th result, partly because Google says it doesn't contain the keyword soft and partly because of its title.

    Adding to that, the relevant SourceForge question that comes up when searching for delete keyword in the official help forum is 12 years old and maybe that's the reason why Google doesn't list it in the first results page.

    Unfortunately, searching Google for just openxava delete (without the soft keyword) is not the first thing that comes to my mind when I'm looking for something known as soft delete, but in that case Google lists the relevant doc as 2nd result. However the misleading title of the page still stands.

    So here is my answer, hoping that Google indexes this sooner or later for others to find this information faster than I did.

    SHORT ANSWER

    In order to implement soft delete in OpenXava you need to add a new custom controller, that modifies the delete action, and a new custom filter, to exlude soft deleted records from the query results.

    LONG ANSWER

    The whole process is officially documented here.