'How to enable/disable a global filter for repository methods in Spring Data Jpa

In my project, all the domain classes have a boolean voided field, to manage the soft delete. Problem is, for all the repository methods, I had to manually override the @Query with voided = false, to get the active entries.

Is there a filtering mechanism in Spring Data JPA to define globally?

For example if the filter is enable, then repository.findAll(); should return all entities with voided = false. If filter is disabled it will return all the data without considering voided.



Solution 1:[1]

The best way I found so far is to use filter inside @Query in the repository against every find method.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gargalatas