Annotation Interface EntityListeners
- directly to an entity class or mapped superclass, to specify its listener classes,
- to a package descriptor to specify listener classes applying to every entity contained in the package, or
- to a module descriptor to specify listener classes applying to every entity contained in any package belonging to the module.
This annotation is an alternative to EntityListener.
Entity listeners declared using this annotation do not need to
be annotated @EntityListener. Callback methods of
listeners declared using this annotation are invoked after
callback methods of listeners declared using EntityListener.
Every entity listener class must have a public constructor with no parameters.
@Entity
@EntityListeners(BookObserver.class)
class Book { ... }
The specified entity listener classes 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.
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.
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:
- 1.0
- See Also:
-
Required Element Summary
Required Elements
-
Element Details
-
value
Class<?>[] valueThe callback listener classes for the annotated entity, in the order in which their lifecycle callback methods are invoked.
-