In JPA, @EntityListeners registers external listener classes whose callback methods the provider invokes at points in an entity's lifecycle: before and after persist, update and remove, and after load. It lets you apply logic such as validation, logging and automatic field updates without touching each entity or service method.
This article focuses on the mechanics: the callback signatures, the order listeners run in, and what the specification forbids inside a callback. For Spring Data auditing see the related article on @EntityListeners and AuditingEntityListener.
A JPA entity is new, managed, detached or removed. Transitions between these states, and flushing changes to the database, raise lifecycle events. Seven callback annotations in jakarta.persistence map to them: @PrePersist, @PostPersist, @PreUpdate, @PostUpdate, @PreRemove, @PostRemove and @PostLoad.
The Pre callbacks run before the provider writes the statement, so they can still change the entity and have the change saved. The Post callbacks run after the statement is issued, and changes made to persistent fields there are not written by that operation.
You can place callback methods directly on the entity, or in a separate listener class named by @EntityListeners.
| Where the method lives | Signature | Typical use |
|---|---|---|
| On the entity | void name() with no arguments | Logic specific to that entity |
| In a listener class | void name(Object entity) or a narrower type | Logic shared by many entities |
A listener method that declares a specific type is only called for entities of that type or its subclasses. Listener classes need a public no-argument constructor and are stateless from the specification's point of view, so do not keep per-entity data in fields.
For a single event the provider calls, in this order:
orm.xml or persistence.xml mapping files.@EntityListeners, in array order.@ExcludeSuperclassListeners and @ExcludeDefaultListeners remove the inherited and default groups. Only one method in a class may carry a given callback annotation.
Pre callback.EntityManager or Query methods, and should not touch other entities. Hibernate flushes while it is already flushing, which can cause errors or lost updates.@PrePersist a workable place for last-minute validation.delete from Ticket or a native query does not trigger them, so any rule you must enforce for every write needs a database constraint or a trigger as well.For cross-aggregate work, publish an application event after commit instead, for example with Spring's @TransactionalEventListener.
The provider creates listener instances. In plain Jakarta EE, CDI can inject into them. In Spring Boot, Hibernate is configured with a bean container, so a listener can have @Autowired or constructor dependencies when it is also a Spring-managed bean. Verify this in your own setup with a test, since it depends on the Hibernate and Spring versions in use.
@PrePersist runs when persist is called, before the insert. @PostPersist runs after the insert statement, so generated identifiers are available there.
At flush time, and only for entities whose state changed. It does not run for a call to save on an unchanged managed entity.
Yes. It is a common place to set @Transient values, such as a display label derived from persistent fields.
No. Listeners are the standard JPA mechanism per entity. Hibernate interceptors and event listeners are provider-specific and cover all entities and more events.
@EntityListeners connects listener classes to the JPA lifecycle callbacks. Choose entity methods for entity-specific logic and listener classes for shared logic, remember the invocation order, avoid persistence operations inside callbacks, and do not rely on callbacks for bulk or native statements.