The @CreatedDate annotation in Spring Data marks a field that Spring fills automatically with the creation timestamp when an entity is first saved. It belongs to the Spring Data auditing feature, so it only works after you enable auditing and register the auditing listener on the entity. Once the entity exists, the value is not changed by later updates; use @LastModifiedDate for that.
It removes the need to set createdAt fields by hand in every service or controller, and it keeps audit data consistent across the whole application.
@CreatedDate does@CreatedDate@CreatedDate does@CreatedDate is declared in org.springframework.data.annotation. When an entity is persisted for the first time, the auditing handler writes the current time into the annotated field. On subsequent updates the field is left alone. Supported field types include java.time.Instant, LocalDateTime, OffsetDateTime, ZonedDateTime, Long/long (epoch milliseconds), java.util.Date and Calendar. Prefer Instant for new code because it has no time zone ambiguity.
Two things are needed: turn auditing on, and attach the listener to the entity.
Keep @EnableJpaAuditing on a dedicated configuration class rather than on the @SpringBootApplication class. Placing it on the main class makes slice tests such as @WebMvcTest fail because the JPA metamodel is then required.
@CreatedDateSpring Boot 3 and later use the Jakarta namespace (jakarta.persistence). The old javax.persistence package from earlier examples no longer applies.
updatable = false is a useful safeguard: even if some code overwrites the field in memory, Hibernate will not include it in UPDATE statements.
Saving a Product through a repository now sets createdDate without any extra code:
Repeating the annotations in every entity is noisy. Put them in a mapped superclass:
Entities then extend Auditable. The @CreatedBy and @LastModifiedBy fields need an AuditorAware bean that tells Spring who the current user is:
@EntityListeners(AuditingEntityListener.class) or @EnableJpaAuditing is not active.LocalDateTime has no zone, so values depend on the JVM default. Use Instant and store in UTC.@EnableMongoAuditing, @EnableR2dbcAuditing or @EnableJdbcAuditing respectively.@CreatedDate change when the entity is updated?No. It is set once at creation. Use @LastModifiedDate for a field that updates with each change.
@EnableJpaAuditing in Spring Boot?Yes. Spring Boot does not enable JPA auditing automatically, so you must add the annotation to a configuration class.
@CreatedDate without Spring Data JPA?The annotation lives in Spring Data Commons. Other Spring Data modules support it with their own enabling annotation. In plain Hibernate you would use @CreationTimestamp instead.
Instant is the safest choice for new code. LocalDateTime works but loses zone information.
@CreatedDate gives you automatic creation timestamps with almost no code. Enable auditing in a configuration class, register AuditingEntityListener on the entity or a mapped superclass, prefer Instant, and add AuditorAware if you also want to record who created the record.