@CreatedDate and @LastModifiedDate are Spring Data auditing annotations that mark entity fields to be filled in automatically with the time a record was created and the time it was last changed. You do not set these fields yourself: Spring Data populates them when you save the entity, as long as auditing is enabled.
They remove repetitive timestamp code from services and keep audit data consistent across all entities. Together with @CreatedBy and @LastModifiedBy, they form the basic audit trail of a Spring Data application.
Both annotations live in org.springframework.data.annotation. They are metadata only; the work is done by an auditing entity listener.
| Annotation | Set when | Typical field type |
|---|---|---|
@CreatedDate | The entity is persisted for the first time | Instant, LocalDateTime, OffsetDateTime |
@LastModifiedDate | The entity is persisted and on every later update | Same types |
Supported types include java.time types (Instant, LocalDateTime, ZonedDateTime, OffsetDateTime), Joda-Time types, java.util.Date and Calendar, and long or Long as epoch milliseconds. Prefer Instant for new code because it has no time zone ambiguity.
Auditing is off by default. You need two things: switch it on with @EnableJpaAuditing, and register the AuditingEntityListener on the entities.
Saving a new Article sets both createdAt and updatedAt. Saving a changed one updates only updatedAt. Marking the creation column updatable = false is a useful safety net at the database level. In Spring Boot 3 and later, the imports use jakarta.persistence; older Boot 2 projects use javax.persistence.
Putting the audit fields in every entity is repetitive. Move them into a @MappedSuperclass and extend it.
Any entity that extends Auditable now gets timestamps with no extra code. Spring Data also ships AbstractAuditable, but a small custom base class gives you control over names and types.
The annotations are shared across Spring Data modules, but each module has its own enabling annotation.
| Module | Enable with |
|---|---|
| Spring Data JPA | @EnableJpaAuditing plus AuditingEntityListener |
| Spring Data MongoDB | @EnableMongoAuditing |
| Spring Data JDBC | @EnableJdbcAuditing |
| Spring Data R2DBC | @EnableR2dbcAuditing |
| Spring Data Elasticsearch | @EnableElasticsearchAuditing |
In MongoDB, JDBC and R2DBC no entity listener is needed; the enabling annotation is enough. In Spring Data MongoDB a version or id field decides whether a document counts as new.
For tests, or to control the time source, expose a DateTimeProvider bean and point to it with dateTimeProviderRef.
For @CreatedBy and @LastModifiedBy you provide an AuditorAware<T> bean that returns the current user, usually from the Spring Security context.
@EnableJpaAuditing is missing, or @EntityListeners(AuditingEntityListener.class) is not on the entity or its mapped superclass.@Query or @Modifying bypass entity callbacks, so @LastModifiedDate is not updated. Set it in the query.null id with a manually assigned key can make Spring Data treat a new entity as existing; check how isNew is determined.Instant column to avoid time zone surprises.Yes. Without it, or without the entity listener on JPA entities, the annotations are ignored and the fields stay null.
Yes, LocalDateTime is supported, but it has no time zone, so values depend on the server zone. Instant or OffsetDateTime is safer.
Yes. On first save both fields receive the current time, unless you disable that with modifyOnCreation = false on @EnableJpaAuditing.
@CreationTimestamp and @UpdateTimestamp are Hibernate-specific. The Spring Data annotations work across modules and integrate with AuditorAware and DateTimeProvider.
@CreatedDate and @LastModifiedDate give you automatic creation and update timestamps. Enable auditing with the module-specific annotation, register AuditingEntityListener for JPA, put the fields in a shared mapped superclass, and prefer Instant. Remember that bulk queries skip the callbacks.