@CreatedDate is not filled in when I save an entity
created_date is null after save()Short answer
Spring Data JPA fills @CreatedDate only when auditing is switched on and the entity is wired to the auditing listener. Check three things: @EnableJpaAuditing on a configuration class, @EntityListeners(AuditingEntityListener.class) on the entity, and a field type the auditing code supports.
Fix it fast
1Find out which cause you have
Save one entity in a test or a debug endpoint and print the audit fields right after save(). If both are null, auditing is not active at all; if only one is, look at that field's type.
var saved = repository.save(new MyEntity()); System.out.println(saved.getCreatedDate() + " / " + saved.getLastModifiedDate());2Match what you see, then jump to its fix
- 1
Every audit field is null, no error in the logAuditing was never enabledFix - 2
@EnableJpaAuditing is present but the fields are still nullThe entity does not use AuditingEntityListenerFix - 3
One field stays null while others are set, or the annotation sits on a type such as StringThe field type is not one auditing supportsFix - 4
Your project uses orm.xml or the jpa:auditing namespace elementUsing XML mapping instead of annotationsFix
- 1
What does the error look like?
created_date is null after save()@CreatedDate and @LastModifiedDate fields stay null
Cause 1 of 4
Auditing was never enabled
1Check
Search the project for the annotation. Nothing in the framework turns auditing on by itself.
grep -rn "EnableJpaAuditing" src/2Fix
Add @EnableJpaAuditing to a configuration class. If you also want @CreatedBy and @LastModifiedBy filled, expose an AuditorAware bean.
@Configuration
@EnableJpaAuditing
class AuditConfig {
}3Verify
Run the app and save an entity again; the dates are now set.
./mvnw -q testConfirmed by docs.spring.io
Cause 2 of 4
The entity does not use AuditingEntityListener
1Check
Open the entity (or its @MappedSuperclass) and look for the entity listener annotation.
grep -rn "AuditingEntityListener" src/2Fix
Put the listener on the entity, or on the base class all your entities extend. Without it nothing observes the persist and update events.
@Entity
@EntityListeners(AuditingEntityListener.class)
public class MyEntity {
@CreatedDate
private Instant createdDate;
@LastModifiedDate
private Instant lastModifiedDate;
}3Verify
Save the entity and print the fields again.
./mvnw -q testConfirmed by docs.spring.io
Cause 3 of 4
The field type is not one auditing supports
1Check
Look at the Java type of each audit field. Supported types are the JDK 8 date and time types, long, Long, and the legacy Date and Calendar.
grep -rn -A1 "@CreatedDate" src/main/java2Fix
Change the field to Instant, LocalDateTime or another supported type, and keep the database column a matching date-time type.
@CreatedDate
private Instant createdDate;3Verify
Save again and read the row back from MySQL.
SELECT created_date FROM my_entity ORDER BY id DESC LIMIT 1;Confirmed by docs.spring.io
Cause 4 of 4
Using XML mapping instead of annotations
1Check
In an XML setup the listener is registered once for all entities in orm.xml, and spring-aspects must be on the classpath.
grep -rn "entity-listener" src/main/resources2Fix
Register AuditingEntityListener under persistence-unit-defaults in orm.xml and add the spring-aspects jar, or switch to the annotation setup above.
<persistence-unit-metadata>
<persistence-unit-defaults>
<entity-listeners>
<entity-listener class="org.springframework.data.jpa.domain.support.AuditingEntityListener" />
</entity-listeners>
</persistence-unit-defaults>
</persistence-unit-metadata>3Verify
Restart and save an entity.
./mvnw -q testConfirmed by docs.spring.io
How do I stop it coming back?
- Put the audit fields and @EntityListeners in one @MappedSuperclass so no entity can forget them.
- Write one small integration test that saves an entity and asserts createdDate is not null.
- Keep the audit column types in the database matched to the Java field types.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
Related fixes
Databases
Hibernate fails on a CREATE or ALTER TABLE statement at startup
Error executing DDL "alter table events drop foreign key FKg0mkvgsqn8584qoql6a2rxheq" via JDBC Statement
Hibernate generated a CREATE, ALTER or DROP statement and MySQL refused it.
3 causes, each with a fixDatabases
Java cannot log in to MySQL 8: authentication plugin caching_sha2_password not supported
ERROR 2061 (HY000): Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.
MySQL 8 accounts use the caching_sha2_password plugin by default, and Connector/J releases before 8.0.9 cannot use it.
3 causes, each with a fixDatabases
Spring Boot stops with 'Unable to build Hibernate SessionFactory'
Could not open JPA EntityManager for transaction
This message only says that JPA could not start.
3 causes, each with a fixDatabases
H2 in PostgreSQL mode still behaves differently from PostgreSQL
Names or null ordering differ from PostgreSQL in test results
H2 compatibility modes are switched on in the JDBC URL and cover only a small part of the differences between databases.
4 causes, each with a fixDatabases
EntityNotFoundException when loading an entity that has a missing related row
ConstraintViolationException on an orphaned foreign key
EntityNotFoundException means Hibernate followed an id and no row was there.
3 causes, each with a fixDatabases
Hibernate cannot initialize a lazy collection or proxy in Spring Boot
could not initialize proxy - no Session
A lazy collection or proxy is loaded on first access, and that needs an open Hibernate session.
3 causes, each with a fix