Member-only story

Quick and Efficient way of Soft Deletion in Hibernate

Namrata
4 min readDec 9, 2024

--

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.

Photo by Ujesh Krishnan on Unsplash

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() {…

--

--

Namrata
Namrata

Written by Namrata

Engineering @Microsoft A software developer writing her daily bits . https://www.linkedin.com/in/namrataagarwal5/

No responses yet