Spring Data JPA - Implementing Basic Auditing by using @CreatedDate and @LastModifiedDate

[Last Updated: Nov 27, 2018]

Spring Data provides following annotations to keep track of who created or changed an entity and when the change happened.

  • @CreatedDate
  • @LastModifiedDate
  • @CreatedBy
  • @LastModifiedBy

This example shows the use of first two annotations. We have to do following things:

  1. In entity class, create date fields and annotate them with @CreatedDate and @LastModifiedDate. The types of these fields can be type Joda-Time, DateTime, legacy Java Date and Calendar, JDK8 date and time types, and long or Long.
  2. Use following on our entity class.
    @EntityListeners is JPA specific annotation.
    AuditingEntityListener.class is Spring Data specific class which implements the listener to capture auditing information on persisting and updating entities.
  3. Annotate our configuration class with @EnableJpaAuditing which enables annotation based auditing.
  4. Also we need to add following extra dependency to activate auditing functionality:



package com.logicbig.example;

import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.time.LocalDateTime;

public class Article {
  private Long id;
  private String content;
  private LocalDateTime dateCreated;
  private LocalDateTime dateModified;


public interface ArticleRepository extends CrudRepository<Article, Long> {

Java Config

public class AppConfig {

  EntityManagerFactory entityManagerFactory() {
      EntityManagerFactory emf =
      return emf;

  public PlatformTransactionManager transactionManager() {
      JpaTransactionManager txManager = new JpaTransactionManager();
      return txManager;

Example client

public class ExampleClient {
  private ArticleRepository repo;

  public void run() {
      //creating and persisting an Article
      Article article = new Article("test article");;

      //load article
      Optional<Article> optArticle = repo.findById(article.getId());
      Article loadedArticle = optArticle.get();

      //have some delay to see a clear diff in modified time
      try {
      } catch (InterruptedException e) {

      //modify article
      loadedArticle.setContent("modified content");;

      //load again

  public static void main(String[] args) {
      AnnotationConfigApplicationContext context =
              new AnnotationConfigApplicationContext(AppConfig.class);
      ExampleClient exampleClient = context.getBean(ExampleClient.class);;
      EntityManagerFactory emf = context.getBean(EntityManagerFactory.class);
Article{id=1, content='test article', dateCreated=2018-11-27T23:51:36.461, dateModified=2018-11-27T23:51:36.461}
Article{id=1, content='modified content', dateCreated=2018-11-27T23:51:36.461, dateModified=2018-11-27T23:51:38.516}

Example Project

Dependencies and Technologies Used:

  • spring-data-jpa 2.1.3.RELEASE: Spring Data module for JPA repositories.
    Uses org.springframework:spring-context version 5.1.3.RELEASE
  • spring-aspects 5.1.3.RELEASE: Spring Aspects.
  • hibernate-core 5.3.7.Final: Hibernate's core ORM functionality.
    Implements javax.persistence:javax.persistence-api version 2.2
  • h2 1.4.197: H2 Database Engine.
  • JDK 1.8
  • Maven 3.5.4

Spring Data JPA basic auditing Select All Download
  • spring-data-jpa-annotation-based-auditing
    • src
      • main
        • java
          • com
            • logicbig
              • example
          • resources
            • META-INF

    See Also