Quick revision
The short answer
With plain JPA, we put createdAt and updatedAt in a @MappedSuperclass with a @PrePersist method that sets both and a @PreUpdate method that refreshes updatedAt, or we move those methods into a separate listener class registered with @EntityListeners. With Spring Data, we enable auditing with @EnableJpaAuditing, register AuditingEntityListener on the entities, and annotate the fields with @CreatedDate and @LastModifiedDate, adding an AuditorAware bean if we also want @CreatedBy. Hibernate's @CreationTimestamp and @UpdateTimestamp are a third option, and in every variant updatedAt changes only when Hibernate detects a real change at flush.
What to remember
In this answer, we filled createdAt and updatedAt in three ways: JPA @PrePersist and @PreUpdate callbacks in a @MappedSuperclass or a listener class, Spring Data auditing with @EnableJpaAuditing, AuditingEntityListener, @CreatedDate and @LastModifiedDate plus AuditorAware for @CreatedBy, and Hibernate's @CreationTimestamp and @UpdateTimestamp. In all of them, updatedAt moves only when Hibernate finds a real change at flush, and bulk SQL bypasses them entirely.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
With plain JPA, we put createdAt and updatedAt in a @MappedSuperclass with a @PrePersist method that sets both and a @PreUpdate method that refreshes updatedAt, or we move those methods into a separate listener class registered with @EntityListeners. With Spring Data, we enable auditing with @EnableJpaAuditing, register AuditingEntityListener on the entities, and annotate the fields with @CreatedDate and @LastModifiedDate, adding an AuditorAware bean if we also want @CreatedBy. Hibernate's @CreationTimestamp and @UpdateTimestamp are a third option, and in every variant updatedAt changes only when Hibernate detects a real change at flush.
Why this question is asked
Almost every table needs to know when a row was created and last changed, and filling those columns by hand in every service method is how they end up wrong or missing. Interviewers ask for both solutions to see whether we know the JPA lifecycle callbacks and Spring Data's auditing support, and when each fits. In this answer, we will recap lifecycle callbacks, write the JPA version with a mapped superclass, move it into a listener class, then write the Spring Data auditing version, mention Hibernate's annotations, and finish with the detail that catches people: when @PreUpdate actually fires.
1A Quick Recap of Lifecycle Callbacks
JPA calls certain methods at fixed moments in an entity's life. @PrePersist runs when persist() is called on a new entity, before the INSERT. @PreUpdate runs during a flush, just before Hibernate writes the UPDATE for an entity it found dirty. There are also @PostPersist, @PostUpdate, @PreRemove, @PostRemove and @PostLoad. A callback can be a method on the entity itself, on a mapped superclass it inherits from, or on a separate listener class.
A @MappedSuperclass is a class whose mapped fields and callbacks are inherited by entities but which is not an entity itself and has no table of its own. That makes it the natural home for columns every entity shares.
2Solution 1: Callbacks in a Mapped Superclass
We put both fields and both callbacks in a base class, and every entity extends it.
Notice updatable = false on createdAt. Even if some code changes the field by mistake, Hibernate leaves the column out of UPDATE statements, so the creation time cannot be overwritten. We use Instant because it is an unambiguous point in time, which maps well to a timestamp with time zone column.
3Solution 1b: A Separate Listener Class
If entities already extend another class, or we want the logic outside the entity, we write a listener class whose callback methods take the entity as a parameter, and register it with @EntityListeners. Here the entities implement a small interface so the listener can work with any of them.
The listener needs a public no-argument constructor, because JPA creates it, and it should not call the EntityManager. To apply it to every entity without annotating each one, it can be declared as a default listener in orm.xml.
4Solution 2: Spring Data JPA Auditing
Spring Data ships a ready-made listener, AuditingEntityListener, that fills fields annotated with @CreatedDate, @LastModifiedDate, @CreatedBy and @LastModifiedBy. We switch it on once with @EnableJpaAuditing and register the listener on the entities, usually through a base class.
The benefit over hand-written callbacks is that the who-did-it part is solved too: AuditorAware supplies the current user, here from Spring Security, and the same annotations work with other Spring Data stores. A DateTimeProvider bean can supply the clock, which makes tests deterministic. By default Spring Data also sets the last-modified fields on creation; check your version if you rely on that.
5Hibernate's Annotations and When @PreUpdate Fires
Hibernate has its own shortcut: @CreationTimestamp and @UpdateTimestamp on the fields, with no callback methods at all. It is the least code, but it ties the entity to Hibernate and does not cover the user who made the change.
| Variant | Portable JPA | Fills the user | Code needed |
|---|---|---|---|
Callbacks in a @MappedSuperclass | yes | no | two small methods |
@EntityListeners class | yes | no | a listener class |
| Spring Data auditing | needs Spring Data | yes, with AuditorAware | config + annotations |
@CreationTimestamp / @UpdateTimestamp | Hibernate only | no | two annotations |
This is the detail to remember for every variant. @PreUpdate, and the equivalent logic in Spring Data auditing and @UpdateTimestamp, runs only when Hibernate detects a change during flush. Calling a setter with the same value, or saving an unchanged entity, does not move updatedAt. Changes made with JPQL bulk UPDATE statements or native SQL bypass the entities entirely, so no callback runs and those statements must set updated_at themselves, or a database trigger must do it.
6Follow-up questions
Why not just use database defaults and triggers?
They catch every write, including bulk SQL, which is an advantage. The cost is logic hidden in the database, values the entity only sees after a refresh, and more work to keep triggers in sync with migrations. Many teams use the application approach for normal writes and set updated_at explicitly in bulk statements.
Can a callback load other entities, for example to set a parent's timestamp?
It should not. The JPA specification says callbacks must not call EntityManager or query operations or change relationships, because they run in the middle of a flush. Changing the entity's own simple fields, as here, is exactly what they are for.
How do we test these timestamps?
Inject a fixed Clock, or a DateTimeProvider for Spring Data auditing, so the expected values are known. Then persist, flush, change a field, flush again and assert both timestamps, plus a case where nothing changes and updatedAt must stay the same.
7Conclusion
In this answer, we filled createdAt and updatedAt in three ways: JPA @PrePersist and @PreUpdate callbacks in a @MappedSuperclass or a listener class, Spring Data auditing with @EnableJpaAuditing, AuditingEntityListener, @CreatedDate and @LastModifiedDate plus AuditorAware for @CreatedBy, and Hibernate's @CreationTimestamp and @UpdateTimestamp. In all of them, updatedAt moves only when Hibernate finds a real change at flush, and bulk SQL bypasses them entirely.
Open with
With plain JPA I would put createdAt and updatedAt in a MappedSuperclass that all entities extend. A PrePersist method sets both to Instant.now, and a PreUpdate method sets updatedAt.
Then, if they want more
I mark createdAt as updatable false, so it can never be overwritten by an update. If the entities cannot share a base class, I move the same two methods into a listener class whose methods take the entity as a parameter, and register it with EntityListeners, or as a default listener in orm.xml.
With Spring Data I would rather use auditing: EnableJpaAuditing on a configuration class, EntityListeners with AuditingEntityListener on a base class, and CreatedDate and LastModifiedDate on the fields. If I also want CreatedBy and LastModifiedBy, I add an AuditorAware bean that returns the current user, for example from Spring Security, and a DateTimeProvider makes the clock testable.
Hibernate also offers CreationTimestamp and UpdateTimestamp, which is the least code but Hibernate-specific. One detail matters in every variant: PreUpdate only runs when Hibernate detects a real change during flush.
Setting a field to the same value or saving an unchanged entity does not move updatedAt, and JPQL bulk updates or native SQL bypass the callbacks completely, so those statements have to set the column themselves.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Fill createdAt and updatedAt on every entity automatically. Show a solution with JPA...": https://scrivio-interview-library.netlify.app/databases/jpa-fundamentals/61011ef0b4/