Auditing with @CreatedDate
Track entity creation and changes automatically.
Auditing with @CreatedDate is a free Spring Boot 4 Microservices & REST APIs lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Spring Boot 4 Microservices & REST APIs learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is JPA Auditing
Auditing automatically tracks who created or modified an entity and when, without manual code in every save.
Spring Data JPA fills audit fields on insert and update for you.
Enabling Auditing
Add @EnableJpaAuditing to a configuration class to activate the feature.
@Configuration
@EnableJpaAuditing
public class JpaConfig {
}Registering the Listener
Annotate audited entities with @EntityListeners(AuditingEntityListener.class) so the callbacks fire.
@Entity
@EntityListeners(AuditingEntityListener.class)
public class Product {
@Id @GeneratedValue
private Long id;
}@CreatedDate
@CreatedDate is set once when the entity is first persisted.
@CreatedDate
private Instant createdAt;@LastModifiedDate
@LastModifiedDate updates on every save, including updates.
@LastModifiedDate
private Instant updatedAt;A Complete Audited Entity
Putting the timestamp fields together.
@Entity
@EntityListeners(AuditingEntityListener.class)
public class Product {
@Id @GeneratedValue
private Long id;
@CreatedDate
private Instant createdAt;
@LastModifiedDate
private Instant updatedAt;
}Auditing the User
@CreatedBy and @LastModifiedBy record who made the change.
@CreatedBy
private String createdBy;
@LastModifiedBy
private String modifiedBy;AuditorAware
To populate the user fields, provide an AuditorAware bean that returns the current principal.
@Bean
public AuditorAware<String> auditorProvider() {
return () -> Optional.of(
SecurityContextHolder.getContext()
.getAuthentication().getName());
}Reusable Base Class
Put audit fields in a @MappedSuperclass so all entities inherit them.
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class Auditable {
@CreatedDate
private Instant createdAt;
@LastModifiedDate
private Instant updatedAt;
}Choosing a Date Type
Audit fields can be Instant, LocalDateTime, or Long. Configure the reference via dateTimeProviderRef if needed.
Verifying Auditing
On the first save createdAt and updatedAt are equal; after an update, only updatedAt changes.
Product p = repository.save(new Product());
// createdAt == updatedAt
p.setName("New");
repository.save(p);
// updatedAt is now later than createdAtQuick Check
Test your understanding of auditing.
Recap
You learned automatic auditing:
@EnableJpaAuditingturns it on@EntityListeners(AuditingEntityListener.class)on entities@CreatedDate/@LastModifiedDatefor timestampsAuditorAwarepopulates@CreatedBy
Frequently asked questions
Is the “Auditing with @CreatedDate” lesson free?
Yes — the full text of “Auditing with @CreatedDate” is free to read here on the web, and the Spring Boot 4 Microservices & REST APIs course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Spring Boot 4 Microservices & REST APIs course, upgrade to CoddyKit PRO.
What will I learn in “Auditing with @CreatedDate”?
Track entity creation and changes automatically. You practise Spring Boot 4 Microservices & REST APIs with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Spring Boot 4 Microservices & REST APIs?
No prior experience is required. Spring Boot 4 Microservices & REST APIs on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Auditing with @CreatedDate” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Spring Boot 4 Microservices & REST APIs lesson?
Yes. Every Spring Boot 4 Microservices & REST APIs lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Dynamic Queries with Specifications
- Projections and DTOs
- Auditing with @CreatedDate
- Pagination and Sorting