Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Spring Data JPA auditing can automatically persist an entity’s creation and last-modification timestamps and, when configured, the users responsible. Enable auditing with @EnableJpaAuditing, register AuditingEntityListener on audited entities or globally, and annotate the fields to populate. Add an AuditorAware bean only if you want to capture user information.

What Spring Data JPA auditing records

Auditing keeps selected metadata on an entity up to date as it is persisted. Use @CreatedDate for its creation time, @LastModifiedDate for its most recent modification time, @CreatedBy for its creator, and @LastModifiedBy for the user who last modified it. Apply only the annotations relevant to your model.

This is an audit stamp, not a complete change history: these fields do not preserve prior values or describe a field-by-field diff. If you need revisions or historical state, design or choose a separate history mechanism.

Configure auditing and register the listener

For Java configuration, enable the infrastructure with @EnableJpaAuditing. The official Spring Data JPA reference also notes that auditing requires spring-aspects.jar. Register AuditingEntityListener either on each audited entity or globally in orm.xml.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Per-entity registration

This approach makes auditing explicit on the entities that use it:

@Entity
@EntityListeners(AuditingEntityListener.class)
class Order {
    @CreatedDate
    private Instant createdDate;

    @LastModifiedDate
    private Instant lastModifiedDate;
}

Global registration

To avoid repeating the listener annotation, register AuditingEntityListener globally in orm.xml, then annotate the metadata fields on the entities to be audited. Use the mapping file’s JPA entity-listener configuration; the exact placement depends on your persistence-unit mapping.

Add created-by and modified-by users

User fields require an AuditorAware<T> bean that supplies the current auditor. Its generic type must match the type of the fields annotated with @CreatedBy and @LastModifiedBy. For example, if those fields are User, provide AuditorAware<User>. If the application has more than one AuditorAware bean, specify the intended bean using the auditorAwareRef attribute of @EnableJpaAuditing.

A Spring Security application can obtain the current principal from SecurityContextHolder. Other applications can provide the current user from their own request, job, or application context. Date-only auditing does not require an AuditorAware bean.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
@Configuration
@EnableJpaAuditing
class JpaAuditConfig {
    @Bean
    AuditorAware<User> auditorProvider() {
        return new SecurityAuditorAware();
    }
}

@Entity
@EntityListeners(AuditingEntityListener.class)
class Order {
    @CreatedBy
    private User createdBy;

    @CreatedDate
    private Instant createdDate;

    @LastModifiedBy
    private User lastModifiedBy;

    @LastModifiedDate
    private Instant lastModifiedDate;
}

SecurityAuditorAware represents your application’s implementation of AuditorAware<User>; it must return the current auditor in the type your entity stores.

Choose where metadata lives and how it is modeled

Auditing fields can be declared directly on an entity or grouped in an embedded object. Annotation-based fields are a flexible, relatively non-invasive choice. Spring Data also supports implementing Auditable or extending AbstractAuditable; those approaches couple the domain model more directly to Spring Data’s auditing API.

Customize timestamp generation when needed

By default, Spring Data uses CurrentDateTimeProvider for audit timestamps. If your application needs a different time source, configure a custom DateTimeProvider and reference it with dateTimeProviderRef, or customize the auditing handler bean. This is useful when timestamps must follow an application-specific clock or time policy.

Configuration choices at a glance

Decision Options When to choose
Metadata Timestamps only, or timestamps plus auditor fields Add an AuditorAware bean only when storing the user responsible for creation or modification.
Listener scope Per entity or global orm.xml registration Use per-entity registration for explicit selection; use global registration to avoid repeating the listener annotation.
Domain model Annotations, embedded metadata, Auditable, or AbstractAuditable Prefer annotations for less invasive and more flexible mapping; use interface or base-class approaches when their stronger coupling suits the model.
Auditor source Security context or another application source Return the current user in the exact type used by the principal fields.
Timestamp source Default CurrentDateTimeProvider or custom provider/handler Customize when the default time source does not meet the application’s policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Implementation checklist

  • Add spring-aspects.jar as required by the Spring Data JPA auditing reference.
  • Enable auditing with @EnableJpaAuditing (or the supported XML configuration).
  • Register AuditingEntityListener globally or on each audited entity.
  • Annotate the timestamp and, if desired, auditor fields.
  • Provide a matching AuditorAware<T> bean for user fields; select it explicitly with auditorAwareRef if multiple candidates exist.
  • Use a separate revision or history design if you need more than current created/modified metadata.

See the Spring Data JPA 4.0 auditing reference for the supported annotations, listener registration, and configuration details.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.