Annotation Interface EntityListeners


@Target({TYPE,PACKAGE,MODULE}) @Retention(RUNTIME) public @interface EntityListeners
Specifies the entity listener classes associated with the annotated class, package, or module. This annotation may be applied:
  • 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:

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
    Modifier and Type
    Required Element
    Description
    Class<?>[]
    The callback listener classes for the annotated entity, in the order in which their lifecycle callback methods are invoked.
  • Element Details

    • value

      Class<?>[] value
      The callback listener classes for the annotated entity, in the order in which their lifecycle callback methods are invoked.