The @EntityListeners annotation attaches one or more listener classes to a JPA entity, so the listener's callback methods run when the entity goes through lifecycle events such as persist, update, remove and load. It keeps cross-cutting logic, above all auditing fields like creation time and last modifier, out of your entities and repositories.
In Spring Data JPA the most common use is AuditingEntityListener, which fills @CreatedDate and @LastModifiedDate fields automatically. You can also write your own listener for logging, validation or derived values.
@EntityListeners is a Jakarta Persistence annotation (jakarta.persistence, formerly javax.persistence) placed on an entity or a mapped superclass. Its value is an array of listener classes. The persistence provider creates an instance of each listener and calls the methods marked with a callback annotation.
| Callback annotation | When it runs |
|---|---|
@PrePersist | Before a new entity is inserted, when persist is called |
@PostPersist | After the insert statement has been executed |
@PreUpdate | Before an update statement for a changed entity |
@PostUpdate | After the update statement |
@PreRemove | Before the entity is removed |
@PostRemove | After the delete statement |
@PostLoad | After the entity is loaded from the database or refreshed |
The post callbacks run when the SQL is issued, which can be before the transaction commits. A later rollback does not undo what your listener did outside the database.
A listener is a plain class with callback methods. Each method takes the entity (or a supertype of it) as its single argument and returns void.
The listener needs a public no-argument constructor. Because the parameter type is Auditable, the same listener serves every entity that implements the interface. Prefer java.time.Instant over java.util.Date, which is the legacy type.
Spring Data JPA ships AuditingEntityListener, so you rarely write timestamp listeners yourself. Enable auditing, add the listener, and annotate fields.
@CreatedBy and @LastModifiedBy need an AuditorAware bean that returns the current user, usually from Spring Security. Extend a @MappedSuperclass that carries these fields and the @EntityListeners annotation to reuse them across entities.
Listeners are inherited from mapped superclasses and entity superclasses. Order of invocation is: default listeners (from orm.xml), then listeners of superclasses, then listeners of the entity, then callback methods declared on the entity itself. Within one @EntityListeners array, listeners run in the order listed.
@ExcludeSuperclassListeners stops inherited listeners for that entity.@ExcludeDefaultListeners stops default listeners declared in the mapping file.EntityManager or Query operations, and must not access other entities, according to the specification. Hibernate may fail or behave unpredictably if you do.update and delete, and native SQL, bypass them.@PreUpdate runs only when the entity is dirty at flush time.It is part of JPA. Spring Data JPA supplies listener implementations, such as AuditingEntityListener, that you attach with it.
Both use the same annotations. A callback method on the entity takes no arguments, while a listener method receives the entity, so the listener can be shared by many types.
They run for each entity persisted or updated through the persistence context, so saveAll triggers them. Bulk @Modifying queries do not.
Envers stores a full revision history in audit tables. Listeners suit simple metadata fields; use Envers when you need to query historical versions.
@EntityListeners moves lifecycle logic out of entities into reusable classes that react to persist, update, remove and load events. Use AuditingEntityListener with @EnableJpaAuditing for standard audit fields, write small listeners for custom rules, and remember that bulk queries skip callbacks and listeners must not run further persistence operations.