Annotation Interface EntityListener
This annotation is an alternative to EntityListeners.
Callback methods of listeners declared using this annotation
are invoked before callback methods of listeners declared
using EntityListeners. However, there is no defined
ordering among listeners declared using this annotation. If
the order of invocation of callback methods is important,
the entity listeners should be declared using the
EntityListeners annotation instead.
Every entity listener class must have a public constructor with no parameters.
@EntityListener(Book.class)
class BookObserver { ... }
The annotated class may have callback methods annotated with any of the standard lifecycle callback annotations:
PostLoad,PrePersist,PostPersist,PreRemove,PostRemove, andPreMerge,PreInsert,PostInsert,PreUpdate,PostUpdate,PreUpsert,PostUpsert,PreDelete, andPostDelete.
A callback method declared by an entity listener class must
have the signature void method(E entity) where E
is an entity class, a mapped superclass, or a supertype of the
entity class or mapped superclass to which the entity listener
applies. If multiple entity classes are assignable to the type
E, the callback method is invoked for any such class to
which the entity listener applies.
@EntityListener
class BookObserver {
@PostPersist
void newBook(Book book) {
...
}
}
An entity listener class may have multiple callback methods for a given type of lifecycle event, but at most one callback method for a given type of event and given parameter type.
@EntityListener
class Observer {
// called only for Books
@PostPersist
void newBook(Book book) {
...
}
// called only for Authors
@PostPersist
void newAuthor(Author author) {
...
}
// called for any entity type to which the listener applies
@PostLoad
void entityLoaded(Object entity) {
...
}
}
Entity listener classes in Jakarta EE environments support
dependency injection through the Contexts and Dependency
Injection (CDI) API when CDI is enabled. An entity listener
class that makes use of CDI injection may also define lifecycle
callback methods annotated with the PostConstruct and
PreDestroy annotations. These methods are called after
dependencies have been injected and before the entity listener
instance is destroyed, respectively.
- Since:
- 4.0
- See Also: