How To Implement Soft Deletes Via SoftDeleteRepository
In Spring Boot Application
Note: Spring Data built-in support for soft deletes is discussed in DATAJPA-307.
Description: This application is an example of implementing soft deletes in Spring Data style via a repository named, SoftDeleteRepository
.
Key points:
- define an
abstract
class,BaseEntity
, annotated with@MappedSuperclass
- in
BaseEntity
define a flag-field nameddeleted
(default this field tofalse
or in other words, not deleted) - every entity that wants to take advantage of soft deletes should extend the
BaseEntity
classs - write a
@NoRepositoryBean
namedSoftDeleteRepository
and extendJpaRepository
- override and implement the needed methods that provide the logic for soft deletes (check out the source code)
- repositories of entities should extend
SoftDeleteRepository