Careeroza
Spring Data JPA Auditing
Why Audit Columns
Support teams ask "when was this order created?", "who changed this price?". Almost every production table has four columns: created_at, updated_at, created_by, updated_by. Spring Data fills them for you, so no service method can forget.
Turn It On
@Configuration
@EnableJpaAuditing // activates AuditingEntityListener support
public class JpaConfig { }@Entity
@Table(name = "products")
@EntityListeners(AuditingEntityListener.class)
public class Product {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private BigDecimal price;
@CreatedDate
@Column(nullable = false, updatable = false)
private Instant createdAt;
@LastModifiedDate
@Column(nullable = false)
private Instant updatedAt;
}Generated SQL
Product p = productRepository.save(new Product("Cotton Kurta", new BigDecimal("799")));
// later, in another transaction
p.setPrice(new BigDecimal("699"));Example output
DEBUG org.hibernate.SQL : insert into products (created_at,name,price,updated_at) values (?,?,?,?) TRACE org.hibernate.orm.jdbc.bind : binding parameter (1:TIMESTAMP_UTC) <- [2026-03-14T04:45:10.112Z] TRACE org.hibernate.orm.jdbc.bind : binding parameter (4:TIMESTAMP_UTC) <- [2026-03-14T04:45:10.112Z] DEBUG org.hibernate.SQL : update products set name=?,price=?,updated_at=? where id=? TRACE org.hibernate.orm.jdbc.bind : binding parameter (3:TIMESTAMP_UTC) <- [2026-03-15T09:02:44.530Z]
Tip: Use Instant (or OffsetDateTime) for audit timestamps and store UTC. Convert to IST only when displaying.
Hibernate's Own Timestamps
Without Spring Data, Hibernate offers @CreationTimestamp and @UpdateTimestamp (package org.hibernate.annotations). They need no listener and no @EnableJpaAuditing, but cannot fill "created by".
@CreationTimestamp
@Column(updatable = false)
private Instant createdAt;
@UpdateTimestamp
private Instant updatedAt;