Quick revision
The short answer
With Hibernate 6.4 or later, we put @SoftDelete on the entity: delete() then becomes an UPDATE of a deleted column and every query and association automatically skips deleted rows; the strategy can be DELETED or ACTIVE and the column name can be changed. On older versions we combine @SQLDelete, which replaces the DELETE statement with an UPDATE, and @SQLRestriction (formerly @Where), which adds a filter to every query. In both cases unique constraints must account for deleted rows and native queries must filter by hand.
What to remember
In this answer, we implemented soft delete for Customer twice. With Hibernate 6.4 and later, @SoftDelete turns deletes into updates and filters every query and association, with DELETED or ACTIVE strategies and a configurable column. On older versions, @SQLDelete replaces the delete statement and @SQLRestriction, formerly @Where, filters reads. Both need the same care around associations that suddenly find nothing, unique constraints that must cover only live rows, and native queries that must filter deleted rows themselves.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
With Hibernate 6.4 or later, we put @SoftDelete on the entity: delete() then becomes an UPDATE of a deleted column and every query and association automatically skips deleted rows; the strategy can be DELETED or ACTIVE and the column name can be changed. On older versions we combine @SQLDelete, which replaces the DELETE statement with an UPDATE, and @SQLRestriction (formerly @Where), which adds a filter to every query. In both cases unique constraints must account for deleted rows and native queries must filter by hand.
Why this question is asked
Soft delete means marking a row as deleted instead of removing it, so it can be restored or kept for history. It is common in customer data, and it is easy to get half right: the delete works, but a report still counts deleted customers, or a new customer cannot register with an old email. Interviewers ask for both the modern and the older way because many code bases still carry the older annotations. In this answer, we will recap what happens on a normal delete, implement soft delete with @SoftDelete, implement it the older way, and then look at the three side effects that catch teams out: associations, unique constraints and native queries.
1A Quick Recap: What repository.delete() Does
When we call customerRepository.delete(customer) in Spring Data JPA, the repository calls EntityManager.remove() on the entity. Hibernate schedules the entity for deletion and, at flush time, sends DELETE FROM customer WHERE id = ?. Nothing in our service code needs to know how that statement is built.
That is what makes soft delete with Hibernate pleasant: we change how the statement is built, not the calling code. The service keeps calling delete(), and Hibernate sends an UPDATE instead. Then, separately, we make every read ignore rows that are marked deleted.
2The Hibernate 6.4 and Later Way: @SoftDelete
Hibernate 6.4 added a dedicated annotation that does both halves at once. We put it on the entity and do not map the deleted column as a field ourselves; Hibernate manages it.
Notice that both the delete and the reads changed, while the repository code did not. By default the strategy is SoftDeleteType.DELETED, which stores true for deleted rows in a column named deleted. We can flip it with @SoftDelete(strategy = SoftDeleteType.ACTIVE), which uses a column (named active by default) that is true for live rows, a style some schemas already have. The columnName attribute renames the column, and a converter attribute lets us store the flag as, for example, Y and N. Newer Hibernate versions also offer a timestamp strategy that stores when the row was deleted; check your version before relying on it.
Because the annotation belongs to the entity, Hibernate applies the filter everywhere it generates SQL for Customer: repository methods, JPQL and criteria queries, and joins from other entities.
3The Older Way: @SQLDelete and @SQLRestriction
Before 6.4 we built the same behaviour from two annotations. @SQLDelete gives Hibernate the exact SQL to run instead of its DELETE, and @SQLRestriction adds a SQL fragment to the WHERE clause of every select for the entity. @SQLRestriction arrived in Hibernate 6.3; on earlier versions the same thing was called @Where, which is now deprecated.
6 messages in order. Play them, or step through them one at a time.
The service still says delete; Hibernate turns it into an update and filters every later read.
There is a catch with @SQLDelete: the SQL is ours, so Hibernate binds parameters in its own order and we must match it. If the entity has a @Version column, Hibernate also binds the version, so the statement needs WHERE id = ? AND version = ? or it fails. Another subtle issue is that the in-memory entity still has deleted = false after the delete, because Hibernate did not run our UPDATE through the entity; this rarely matters since the entity is removed from the persistence context, but it surprises people in tests.
4Side Effects That Catch Teams Out
First, associations are filtered too. If an Order has @ManyToOne Customer and the customer is soft deleted, loading that customer through the order can find nothing, and depending on the mapping Hibernate either returns null or throws an exception for a missing row. Collections of customers skip deleted ones. That is usually what we want for live screens, but code that needs all rows, such as an invoice reprint for a closed account, has to read them another way, for example with a native query or a separate read-only entity mapped without the restriction.
Second, unique constraints still see deleted rows. If email is unique, a customer who deleted their account cannot register again, because the old row still holds the email. On PostgreSQL the fix is a partial unique index that only covers live rows. MySQL has no partial indexes, so there we use a generated column that is null for deleted rows and make that unique.
Third, native queries bypass all of this. The restriction lives in Hibernate's SQL generation, so a native query such as SELECT count(*) FROM customer counts deleted rows as well. Every native query and every report written directly in SQL must add WHERE NOT deleted itself, which is why many teams also create a database view of live customers for reporting tools.
5Follow-up questions
Can we restore a soft-deleted customer with @SoftDelete?
Not through normal entity operations, because Hibernate hides the row from every query. Restoring needs a native UPDATE that sets the flag back, or a separate admin mapping without the annotation. If undo is a frequent feature, that is worth designing explicitly rather than discovering later.
Does a JPQL bulk DELETE also become an update?
With @SoftDelete, Hibernate applies the soft delete to mutation queries it generates for the entity, so check the SQL log for your version. With @SQLDelete it does not: @SQLDelete only replaces the statement for em.remove(), and a JPQL DELETE FROM Customer still physically deletes rows.
Should we index the deleted column?
Usually not on its own, since it has only two values. If most rows are live, the partial indexes on the columns we actually filter by, built with WHERE NOT deleted, serve the common queries better.
6Conclusion
In this answer, we implemented soft delete for Customer twice. With Hibernate 6.4 and later, @SoftDelete turns deletes into updates and filters every query and association, with DELETED or ACTIVE strategies and a configurable column. On older versions, @SQLDelete replaces the delete statement and @SQLRestriction, formerly @Where, filters reads. Both need the same care around associations that suddenly find nothing, unique constraints that must cover only live rows, and native queries that must filter deleted rows themselves.
Open with
On Hibernate 6.4 or later I'd just annotate Customer with @SoftDelete. Then repository.delete runs an UPDATE that sets the deleted column to true, and every query Hibernate generates for Customer, including joins from other entities, adds deleted = false.
Then, if they want more
The default strategy is DELETED with a column called deleted, but I can switch to the ACTIVE strategy or rename the column, and I don't map the flag as a field myself.
On older versions I'd combine two annotations: @SQLDelete with UPDATE customer SET deleted = true WHERE id = ?, which replaces the delete statement, and @SQLRestriction with deleted = false, which used to be @Where before 6.3. With @SQLDelete I have to watch parameter order, for example adding the version if the entity has @Version.
Then I'd mention three side effects. Associations to a deleted customer are filtered too, so code that needs all rows must read them another way. Unique constraints still see deleted rows, so on PostgreSQL I'd make the email index partial, WHERE NOT deleted, and on MySQL use a generated column.
And native queries and reports don't get the filter automatically, so they must add it, or use a view of live rows.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Hibernate 6
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Implement soft delete for a Customer entity so that repository.delete(customer) sets...": https://scrivio-interview-library.netlify.app/databases/jpa-fundamentals/31a9b6d87f/