Files
spring-data-demo/jpa-auditing

jpa-auditing

Companion code for JPA Auditing in Spring Boot 4: @CreatedDate, @LastModifiedBy and the Current User on ankurm.com.

Component Version
Spring Boot 4.1.2
Spring Data JPA / Commons 4.1.2 / 4.1.2
Spring Framework 7.0.10
Spring Security 7.1.2
Hibernate ORM / Envers 7.4.13.Final / 7.4.13.Final
H2 (in-memory) 2.4.240
JDK Temurin 25.0.5 LTS

No Docker needed. Every test writes what it saw to output/ while asserting the same facts, so the numbers in the post fail the build if they stop being true. Timestamps are deterministic because AuditingConfig supplies a settable DateTimeProvider clock.

./scripts/run-all.sh      # regenerates every file in output/
./scripts/time-zones.sh   # only the Instant vs LocalDateTime transcript, under three JVM zones

Surefire forks a fresh JVM per test class (reuseForks=false) on purpose: the "auditing not enabled" test is only trustworthy in a JVM that never started a context with auditing on.

Source file What it shows
AuditingConfig @EnableJpaAuditing with an AuditorAware and a DateTimeProvider; the demo switches
Auditable the four audit columns in a @MappedSuperclass with AuditingEntityListener
SecurityContextAuditorAware the safe AuditorAware (empty for anonymous)
NaiveAuditorAware the common version that stores anonymousUser
ArticleRepository bulk @Modifying updates that bypass auditing, and the stamped fix
LocalStamp LocalDateTime vs Instant for the same @CreatedDate
ForgottenListener an entity that forgot @EntityListeners
Document, AuditRevision, UsernameRevisionListener Envers history with the user per revision
Transcript Shows
output/01-create-update.txt what is written on create and on update, and the UPDATE SQL
output/02-no-change-no-stamp.txt an unchanged entity is not touched
output/03-bulk-update-bypass.txt a bulk update skips auditing; the stamped version does not
output/04-auditing-not-enabled.txt no @EnableJpaAuditing: annotations are inert
output/05-missing-entity-listener.txt missing @EntityListeners: null, no error
output/06-naive-auditor-anonymous.txt the anonymousUser trap
output/07-safe-auditor-two-users.txt anonymous, alice and bob over HTTP Basic
output/08-other-thread.txt the user is lost on another thread unless the context is carried
output/09-instant-vs-localdatetime-*.txt the same moment in three JVM zones
output/10-envers-history.txt one revision per change, with the user
output/11-modifying-needs-transaction.txt a custom @Modifying method needs @Transactional