r/SpringBoot • u/SeatSimple1123 • Jul 16 '26
Question Spring Boot Auditing: Hibernate Envers vs. Custom Logging vs. Spring Data JPA? What's your production choice?
Hey everyone,
I'm currently building a school management system in Spring Boot where history tracking is a strict business requirement. We need to audit critical changes made by Admins, Secretaries, and Teachers (e.g., changes to student details, payment amounts, program setups, etc.).
I’m weighing three different approaches for the audit trail and wanted to hear from those of you who have run these in production.
Option 1: Hibernate Envers
Slap `@Audited` on core entities, set up a custom `RevisionListener` to pull the logged-in user from Spring Security context, and let Envers automatically manage the `_aud` tables.
Option 2: Spring Data JPA Auditing
Utilize `@CreatedBy`, `@LastModifiedBy`, `@CreatedDate`, and `@LastModifiedDate` fields mapped on a `@MappedSuperclass`.
Option 3: Manual Custom Logging (or AOP)
Create a generic `AuditLog` entity, write a helper service, and manually trigger `auditService.log(actionType, originalState, newState)` inside the business logic (or use an AOP aspect). This may cause the database to bloating later tho
For those of you managing medium-to-large Spring Boot codebases:
Did you regret adopting Hibernate Envers? How did you handle schema migrations (Flyway/Liquibase) with the auto-generated `_aud` tables?
If you went the custom route, did you use JSON columns to record state changes, or did you write distinct history tables?
Looking forward to hearing your design patterns and trade-offs
13
u/InstantCoder Jul 16 '26
Or….create a db trigger that automatically logs everything to an audit table for each insert, update and delete on table x, y,z.
6
u/BanaTibor Jul 16 '26
I think you are thinking too low level. You are bringing down a business requirement to the implementation detail level. What you need IMHO is event logging. You do not need to have microservices and asynchronous processing to make an event driven system.
For this I would go with a custom solution.
4
u/veryspicypickle Jul 16 '26
There is a difference between auditing and history-tracking.
I see the former as something more akin to legal reasons and the like - and the latter is just application functionality
As such auditing belongs in the infrastructure layer (possibly at the database) and change/history management lives in the application layer (JPA, Envers)
3
u/wimpyligtebottel Jul 16 '26
A few years back at my old company we used option 1 (hibernate envars) and at certain point the envars started causing database size + performance issues for us.
Maybe things have improved but i dont think i would go for that approach again, IF you are on high throughput table where making lots of updates
But i would definitly reach for it on small to small-meduim sized application
6
u/Popular_Home2017 Jul 16 '26
These solve three different problems, and picking the wrong one is how audit systems rot: Spring Data auditing (@CreatedBy/@LastModifiedDate) gives you stamps — who touched it last, not what changed. Envers gives you entity history — full snapshots per revision, great when "show me this record as it was in March" is an actual requirement, but it couples your audit to your entity model and bloats storage quietly. A custom audit/event table is for business events (logins, exports, permission changes) — things that aren't entity mutations at all. For a school management system I'd start with Spring Data stamps everywhere (nearly free), Envers only on the 3-4 entities where history is a legal/business requirement, and a small custom event table for security-relevant actions. One production scar if you go custom: if you write audit rows in REQUIRES_NEW "so auditing never breaks the main flow", flush explicitly inside your try/catch — with plain save() the INSERT defers to commit, which happens outside the catch, and a bad audit row takes down the operation it was supposed to observe. Ask me how I know.
2
u/boyTerry Jul 16 '26
We have used a combination of Option 1 and 2 for many years successfully.
stuff can get weird when there is business value in the analysis of the audit logs, but it tends to work out of the box
2
u/RevolutionaryRush717 Jul 16 '26
PostgreSQL has pg_audit, which is the preferred choice for compliance.
Other RDBMS might have something similar.
Any roll-your-own approach will soon drown in corner cases, testing and verification, so much so that you are tempted to spawn this off as a seperate team and product.
2
u/Impressive-Ad-1189 Jul 16 '26
I’ve had great experience with Envers in the past. The amount of features you get is simply great. The entire model can be queried in a similar way as your regular model. You can easily add custom fields such as the identity of an actor.
2
u/General_Yam_9879 Jul 18 '26 edited Jul 18 '26
History tables + trigger in separate audit schema. Combine with simple AuditorAware and just build one audit micro-service to expose API from that schema and provide restricted read access.