jpa-auditing: Spring Data JPA auditing on Boot 4.1 - @CreatedDate/@LastModifiedBy, AuditorAware with Spring Security, Instant vs LocalDateTime, Envers
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b8789qiV9Fsv2FqZde
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
# create as alice at 2026-11-17T08:00:00Z
|
||||
createdAt=2026-11-17T08:00:00Z updatedAt=2026-11-17T08:00:00Z
|
||||
createdBy=alice updatedBy=alice
|
||||
|
||||
# update as bob at 2026-11-17T10:00:00Z
|
||||
createdAt=2026-11-17T08:00:00Z updatedAt=2026-11-17T10:00:00Z
|
||||
createdBy=alice updatedBy=bob
|
||||
UPDATE SQL: update article set body=?,title=?,updated_at=?,updated_by=? where id=?
|
||||
@@ -0,0 +1,3 @@
|
||||
# save an entity whose fields did not change, clock moved to 12:00Z
|
||||
UPDATE statements issued: 0
|
||||
updatedAt=2026-11-17T08:00:00Z (still the create time)
|
||||
@@ -0,0 +1,6 @@
|
||||
# @Modifying JPQL update at 09:30Z as alice
|
||||
SQL: update article a1_0 set title=? where a1_0.id=?
|
||||
title=Renamed (bulk) updatedAt=2026-11-17T08:00:00Z updatedBy=alice
|
||||
|
||||
# the same update, setting the audit columns in the query itself, at 09:45Z
|
||||
title=Renamed (stamped) updatedAt=2026-11-17T09:45:00Z updatedBy=alice
|
||||
@@ -0,0 +1,4 @@
|
||||
# demo.auditing.enabled=false (i.e. no @EnableJpaAuditing)
|
||||
AuditingConfig bean present: false
|
||||
save() threw: org.springframework.dao.DataIntegrityViolationException
|
||||
root cause: not-null property references a null or transient value for entity com.ankurm.jpaauditing.Article.createdAt
|
||||
@@ -0,0 +1,3 @@
|
||||
# @EnableJpaAuditing is ON, but the entity has @CreatedDate and no @EntityListeners
|
||||
saved id=1 createdAt=null
|
||||
exception: none
|
||||
@@ -0,0 +1,2 @@
|
||||
# demo.auditor=naive, POST /articles with NO credentials
|
||||
{"id":1,"title":"From REST","createdBy":"anonymousUser","updatedBy":"anonymousUser","createdAt":"2026-11-17T08:00:00Z","updatedAt":"2026-11-17T08:00:00Z"}
|
||||
@@ -0,0 +1,8 @@
|
||||
# demo.auditor=safe, POST /articles with NO credentials
|
||||
{"id":1,"title":"From REST","createdBy":null,"updatedBy":null,"createdAt":"2026-11-17T08:00:00Z","updatedAt":"2026-11-17T08:00:00Z"}
|
||||
|
||||
# POST as alice (HTTP Basic)
|
||||
{"id":2,"title":"From REST","createdBy":"alice","updatedBy":"alice","createdAt":"2026-11-17T08:00:00Z","updatedAt":"2026-11-17T08:00:00Z"}
|
||||
|
||||
# PUT as bob
|
||||
{"id":2,"title":"Edited by bob","createdBy":"alice","updatedBy":"bob","createdAt":"2026-11-17T08:00:00Z","updatedAt":"2026-11-17T08:00:00Z"}
|
||||
@@ -0,0 +1,5 @@
|
||||
# test method runs as alice
|
||||
auditor on the request thread : Optional[alice]
|
||||
auditor on a plain pool thread: Optional.empty
|
||||
createdBy of row saved on a plain pool thread : null
|
||||
createdBy of row saved via DelegatingSecurityContextExecutor: alice
|
||||
@@ -0,0 +1,5 @@
|
||||
JVM zone: America/New_York (clock instant: 2026-11-17T08:00:00Z)
|
||||
column CREATED_INSTANT : TIMESTAMP WITH TIME ZONE
|
||||
column CREATED_LOCAL : TIMESTAMP
|
||||
stored LocalDateTime : 2026-11-17T03:00
|
||||
stored Instant : 2026-11-17T08:00:00Z
|
||||
@@ -0,0 +1,5 @@
|
||||
JVM zone: Asia/Kolkata (clock instant: 2026-11-17T08:00:00Z)
|
||||
column CREATED_INSTANT : TIMESTAMP WITH TIME ZONE
|
||||
column CREATED_LOCAL : TIMESTAMP
|
||||
stored LocalDateTime : 2026-11-17T13:30
|
||||
stored Instant : 2026-11-17T08:00:00Z
|
||||
@@ -0,0 +1,5 @@
|
||||
JVM zone: UTC (clock instant: 2026-11-17T08:00:00Z)
|
||||
column CREATED_INSTANT : TIMESTAMP WITH TIME ZONE
|
||||
column CREATED_LOCAL : TIMESTAMP
|
||||
stored LocalDateTime : 2026-11-17T08:00
|
||||
stored Instant : 2026-11-17T08:00:00Z
|
||||
@@ -0,0 +1,7 @@
|
||||
# the row itself (Spring Data auditing): only the latest stamps
|
||||
content=v3 createdBy=alice updatedBy=alice updatedAt=2026-11-17T10:00:00Z
|
||||
|
||||
# the history (Envers): one row per revision
|
||||
rev 1 revision.username=alice content=v1 updatedBy=alice updatedAt=2026-11-17T08:00:00Z
|
||||
rev 2 revision.username=bob content=v2 updatedBy=bob updatedAt=2026-11-17T09:00:00Z
|
||||
rev 3 revision.username=alice content=v3 updatedBy=alice updatedAt=2026-11-17T10:00:00Z
|
||||
@@ -0,0 +1,3 @@
|
||||
# @Modifying(flushAutomatically = true) method with no @Transactional, called from plain code
|
||||
exception: org.springframework.dao.InvalidDataAccessApiUsageException
|
||||
message : No EntityManager with actual transaction available for current thread - cannot reliably process 'flush' call
|
||||
Reference in New Issue
Block a user