How JPA auditing works
JPA auditing automatically populates audit fields on your entities when they are persisted or updated. OrgStack implements this through three key components:Enable JPA auditing
The This configuration tells Spring to scan for entities with auditing annotations and automatically populate their audit fields.
@EnableJpaAuditing annotation activates Spring Data JPA’s auditing infrastructure:com/orgstack/config/JpaConfig.java
Create a base entity with audit fields
The
BaseEntity class provides common fields for all entities, including audit timestamps:com/orgstack/common/BaseEntity.java
The
@MappedSuperclass annotation indicates that this class should not be mapped to its own database table, but its fields should be inherited by subclasses.Key auditing annotations
@EntityListeners
AuditingEntityListener, which intercepts entity lifecycle events (persist, update) and populates audit fields. It’s applied to BaseEntity so all subclasses inherit this behavior.
@CreatedDate
@CreatedDate annotation automatically sets this field to the current timestamp when an entity is first persisted. The updatable = false constraint ensures it never changes after initial creation.
@LastModifiedDate
@LastModifiedDate annotation updates this field to the current timestamp whenever the entity is modified. This provides a complete audit trail of when changes occurred.
UUID-based identity
OrgStack uses UUIDs instead of auto-incrementing integers for entity IDs:UUIDs are generated in the application layer (not the database) during entity construction. This approach has several benefits:
- Predictable IDs: You know the entity ID before persisting to the database
- No ID collisions: Safe to generate IDs across distributed systems
- Security: Harder to enumerate resources compared to sequential integers
Using Instant for timestamps
OrgStack usesjava.time.Instant for all timestamp fields:
Benefits of automatic auditing
Consistency
Every entity gets the same audit fields with the same behavior. No risk of forgetting to set timestamps manually.
Accuracy
Timestamps are set automatically by the framework at the exact moment of persistence, eliminating human error.
Simplicity
Your service layer doesn’t need timestamp management logic. Just save entities and JPA handles the rest.
Compliance
Immutable creation timestamps (
updatable = false) provide tamper-proof audit records.What gets audited automatically
With this setup, you get automatic tracking for:- Entity creation:
createdAtis set once when the entity is first saved - Entity updates:
updatedAtis refreshed on every save operation - Immutable records:
createdAtandidcan never be changed after creation
Next steps
Tenant isolation
Learn how OrgStack isolates data between tenants
JWT authentication
Understand how authentication works with Spring Security