Member-only story
Soft deletion is a technique often used in database management to logically remove records while retaining them in the database for auditing, restoring, or other purposes. Instead of deleting records physically, a flag or marker (e.g., deleted = true
) is used to indicate that a record is no longer active.
It enables easy data recovery from accidental deletions and supports auditing by retaining a history of changes. Soft delete aligns with regulatory requirements for data retention and allows for features like undoing deletions or viewing archived content. It’s cost-effective, avoiding complex restoration processes, and offers flexibility in filtering data visibility based on user roles or application needs.
In Hibernate, soft deletion can be implemented efficiently using annotations like @SQLDelete
and @Where
. This article explains how to implement soft deletion in a Hibernate-centric approach.
The foundation of soft deletion is a base class that contains the deleted
flag. This class is annotated with @MappedSuperclass
, allowing other entities to inherit its properties:
@MappedSuperclass
public abstract class BaseEntity {
@Column(name = "deleted")
protected boolean deleted = false; // Default to false (not deleted)
public boolean isDeleted() {…