Hibernate: @CreationTimestamp and @UpdateTimestamp

Auditing columns without writing a listener, set by Hibernate rather than by the database.

Code
@Entity
class Order {
    @CreationTimestamp
    Instant createdAt;

    @UpdateTimestamp
    Instant updatedAt;
}

// Spring Data equivalent, with @EnableJpaAuditing:
@CreatedDate Instant createdAt;
@LastModifiedDate Instant updatedAt;
@CreatedBy String createdBy;
Output
insert into orders (created_at, updated_at) values (?, ?)
Both values come from the application clock, not the database clock -
which matters when the two are in different time zones.
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-11