DEV Community

Cover image for ⏱️ I Was Manually Setting createdAt & updatedAt… Until I Learned Spring Boot Auditing
Shashwath S H
Shashwath S H

Posted on

⏱️ I Was Manually Setting createdAt & updatedAt… Until I Learned Spring Boot Auditing

Auditing

🔥 Backup options:

  • “Stop Writing createdAt/updatedAt Manually in Spring Boot”
  • “Spring Boot Auditing: The Feature That Makes Your Backend Look Professional”
  • “How I Automated Entity Timestamps in Spring Data JPA”

When I started building CRUD APIs with Spring Boot, everything worked fine.

But one thing kept bothering me.

Every entity needed fields like:

  • createdAt
  • updatedAt
  • createdBy
  • updatedBy

And I was setting them manually.

Again and again.
In every service method.
For every entity.

That’s when I discovered Auditing in Spring Boot — and it instantly made my backend feel more real-world and production-ready.


🧠 What is Auditing in Spring Boot?

Auditing in Spring Boot allows you to automatically populate fields such as:

Creation timestamp
Last modification timestamp
Created by user
Last modified by user

So instead of manually updating these fields, Spring does it for you automatically.


✅ Why Auditing is Actually Useful

Auditing helps in:

  • tracking data history
  • debugging changes
  • maintaining clean records
  • building professional enterprise systems

Most real applications require this, especially when multiple users are modifying records.


🛠️ Steps to Add Auditing in Spring Boot

1️⃣ Create an Auditable Base Entity

Create a superclass and add:

@EntityListeners(AuditingEntityListener.class)

This gives access to:

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

2️⃣ Extend All Entities from the Superclass

Instead of repeating auditing fields in every entity, simply extend the base class.

This keeps the code:
✅ clean
✅ reusable
✅ consistent


3️⃣ Implement AuditorAware

Create a class that implements AuditorAware.

Its job is to provide:
✅ the current authenticated user (usually from Spring Security)

This is how Spring knows who created/modified the entity.


4️⃣ Enable JPA Auditing

Enable auditing using:

@EnableJpaAuditing

This is usually added in a configuration class.


5️⃣ Pass the AuditorAware Bean Reference

Connect your auditor provider to auditing configuration so Spring can use it globally.


🔍 Internal Working of Auditing (Simple Explanation)

Auditing works using an entity listener called AuditingEntityListener.

Whenever an entity is saved or updated, Spring triggers:

PrePersist (before insert)
PreUpdate (before update)

So when an entity is:

  • created → created fields are filled automatically
  • updated → last modified fields are updated automatically

The user information comes from AuditorAware.


🚀 Final Thoughts

Spring Boot Auditing is one of those features that quietly improves your backend quality.

If you’re still:

  • manually setting timestamps
  • repeating createdAt/updatedAt logic everywhere
  • struggling to track modifications

Start using Auditing.

It makes your project look professional and enterprise-ready.

This post is part of my learning-in-public journey while exploring Spring Boot and real-world backend development.

Do you handle createdAt/updatedAt manually or are you using Spring Boot Auditing?

Top comments (0)