Quick revision
The short answer
Spring Data auditing only stores the latest creator, modifier and timestamps on the row, so it cannot meet a full-history requirement. Hibernate Envers, enabled with @Audited, writes a revision row for each change made through Hibernate, but bulk updates and native SQL bypass it; database triggers capture every change regardless of source but are harder to version and test; and change data capture such as Debezium streams changes from the database log into an audit store without touching application code. The choice then rests on retention for seven years, storage cost, how history will be queried and how tamper-resistant the store must be.
What to remember
In this answer, we saw that Spring Data auditing records only the last change, Envers records history for changes made through Hibernate but misses bulk, native and external writes, triggers capture every write at the cost of versioning, testing and write latency, and CDC captures every committed change from the log without code changes but needs infrastructure and a way to carry the user. For a seven-year regulatory history, coverage of all write paths, retention planning, query design and an append-only, tamper-resistant store decide the design.
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.
Spring Data auditing only stores the latest creator, modifier and timestamps on the row, so it cannot meet a full-history requirement. Hibernate Envers, enabled with @Audited, writes a revision row for each change made through Hibernate, but bulk updates and native SQL bypass it; database triggers capture every change regardless of source but are harder to version and test; and change data capture such as Debezium streams changes from the database log into an audit store without touching application code. The choice then rests on retention for seven years, storage cost, how history will be queried and how tamper-resistant the store must be.
Why this question is asked
Regulatory audit requirements look simple, who changed what and when, but they are unforgiving. A history that misses changes made by a nightly batch job, or that an administrator could edit, fails the audit even if it covers 99 percent of updates. Interviewers ask this design question to see whether we can compare mechanisms by what they actually capture, not by how easy they are to add, and whether we think about the long tail: seven years of data, queries by auditors and proof that nobody tampered with it. In this answer, we will separate last-change auditing from history, look at each mechanism with a small example, compare them on coverage and cost, and work through retention, querying and tamper resistance.
1A Quick Recap: Last Change Versus History
There are two different needs hidden in the word auditing. The first is metadata on the current row: created_at, created_by, updated_at, updated_by. It answers who touched this record last. The second is a history: every version of the record, or every change, with who, when and what the values were before and after. Only the second answers what the address was on 3 March 2024 and who changed it.
A regulator asking for a complete change history over seven years wants the second. The candidates are Spring Data auditing, Hibernate Envers, database triggers and change data capture, and they differ mainly in which changes they see.
2Spring Data Auditing: Only the Last Change
Spring Data's auditing fills @CreatedDate, @CreatedBy, @LastModifiedDate and @LastModifiedBy through a JPA entity listener. Each update overwrites the last-modified values on the same row. After three changes by three people, the row shows only the third. It is useful metadata and every history solution can coexist with it, but it is not history and cannot meet this requirement on its own.
3Hibernate Envers: History From Hibernate
Envers is a Hibernate module. Annotating an entity with @Audited makes Hibernate write, for every insert, update and delete it performs, a row into a matching audit table, such as customer_aud, with the entity's values and a revision number. A shared revinfo table records each revision's timestamp, and a custom revision entity can add the user and a correlation id.
Envers is attractive because it is a few annotations and its query API answers what did this look like at revision N. But it only sees changes that pass through Hibernate's entity lifecycle. A @Modifying JPQL bulk update, native SQL, a migration script, a DBA fixing data by hand or another application writing to the same table leaves no trace. For a regulator, those gaps matter.
4Database Triggers: Every Write, Inside the Database
A trigger is code in the database that runs on every insert, update or delete of a table, whatever sent it. A history trigger copies the old and new row into an audit table. Because it runs inside the database, it catches application writes, bulk SQL, scripts and other applications alike.
The costs are elsewhere. Trigger code lives in the database, so it must be versioned through migrations and tested with integration tests, which teams often neglect. The database knows only its own user, not the application's end user, so the application must pass the user in, here through a session setting app.user that each transaction sets. And every write now does two writes in the same transaction, which adds latency to hot tables.
5Change Data Capture: Reading the Database Log
Every relational database keeps a log of changes for recovery and replication: the write-ahead log in PostgreSQL, the binlog in MySQL. Change data capture (CDC) tools such as Debezium read that log and publish each committed change, with before and after values, as an event, usually to Kafka. A consumer writes the events into an audit store. Because the log records every committed change, CDC sees all write paths, and the application code does not change at all.
6 messages in order. Play them, or step through them one at a time.
Every committed write reaches the log, whatever sent it.
6Comparing the Options
CDC has two gaps of its own. The log does not know the end user, so the application must record it somewhere that reaches the log, such as an updated_by column on the row or a small transaction metadata table written in the same transaction. And CDC adds infrastructure: connectors, Kafka and a consumer, which need operating and monitoring, including alerts when the connector lags.
| Option | Captures | Main cost |
|---|---|---|
| Spring Data auditing | Last change only; no history | Cannot meet this requirement |
| Hibernate Envers | Changes made through Hibernate | Misses bulk, native and external writes |
| Database triggers | Every write to the table | Versioning, testing, write latency |
| CDC (Debezium) | Every committed write, from the log | Infrastructure; user must reach the log |
7Retention, Querying and Tamper Resistance
Seven years is long. If customer has 5 million rows and each changes ten times a year, that is 350 million history records over seven years; at about 1 KB each, roughly 350 GB before indexes. Keep recent history in a queryable store and move older years to cheaper storage, such as partitioned tables or compressed files in object storage, and define how deletion works at the end of retention, especially where privacy laws require personal data to be erased.
Querying matters too: auditors will ask for the history of one customer, or all changes by one user in a period. Index the history by entity id and time, and by actor, and test those queries before the audit.
Tamper resistance is the point many designs miss. A history table that application users or DBAs can update proves little. Write history to an append-only store with separate credentials, deny update and delete on it, and consider hash-chaining records, each storing a hash of the previous one, or using storage with write-once retention locks. My choice for this requirement would be CDC, or triggers for a smaller system, into a separate append-only store, with the user recorded on each write, and Envers only if we can prove that every write goes through Hibernate.
8Follow-up questions
Can Envers and CDC be combined?
Yes, but usually there is no need. Envers is convenient for showing history inside the application; CDC is the complete record.
How do you get the end user into a CDC event?
Make sure it is written in the same transaction as the change: an updated_by column on the row, filled by Spring Data auditing, or a small table recording the transaction id, user and correlation id. The consumer joins them by transaction id.
9Conclusion
In this answer, we saw that Spring Data auditing records only the last change, Envers records history for changes made through Hibernate but misses bulk, native and external writes, triggers capture every write at the cost of versioning, testing and write latency, and CDC captures every committed change from the log without code changes but needs infrastructure and a way to carry the user. For a seven-year regulatory history, coverage of all write paths, retention planning, query design and an append-only, tamper-resistant store decide the design.
Open with
First I would separate two needs. Spring Data auditing gives created and modified columns on the row, so it only knows the latest change; it cannot provide history, so on its own it fails this requirement.
Then, if they want more
For real history there are three candidates. Hibernate Envers is the easiest: @Audited makes Hibernate write a revision row for every change, and a revision entity can store the user.
But it only sees writes that go through Hibernate's entity lifecycle, so bulk JPQL updates, native SQL, scripts and other applications leave no trace, and a regulator will find those gaps.
Database triggers see every write to the table regardless of source, which is a big plus, but the code lives in the database, so it must be versioned in migrations and tested, the application has to pass the end user through a session setting, and every write costs a second write.
Change data capture, like Debezium reading the PostgreSQL WAL, sees every committed change with before and after values and needs no application changes; the costs are the Kafka and connector infrastructure, and making sure the user is recorded in the same transaction, for example in an updated_by column.
For a regulated seven-year history, I would pick CDC, or triggers for a smaller system, feeding a separate append-only audit store.
Then I would plan retention, with older years moved to cheaper storage, index history by entity and actor so auditors' queries are fast, and make the store tamper-resistant with separate credentials, no update or delete rights, and possibly hash chaining.
About this answer
- Last updated
- 12 November 2026
- 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 "A regulator requires a complete change history for customer records: who changed what...": https://scrivio-interview-library.netlify.app/databases/spring-data-jpa/2776b92106/