Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

@CreatedDate is not filled in when I save an entity

Errorcreated_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.

  • Applies to Spring Boot 3.x, Spring Data JPA 3.x, Java 21, MySQL 8
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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());
  2. 2Match what you see, then jump to its fix

    1. 1Every audit field is null, no error in the logAuditing was never enabledFix
    2. 2@EnableJpaAuditing is present but the fields are still nullThe entity does not use AuditingEntityListenerFix
    3. 3One 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. 4Your project uses orm.xml or the jpa:auditing namespace elementUsing XML mapping instead of annotationsFix

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 test

Confirmed by docs.spring.io

Did this fix it?

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 test

Confirmed by docs.spring.io

Did this fix it?

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/java

2Fix

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

Did this fix it?

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/resources

2Fix

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 test

Confirmed by docs.spring.io

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.