Entity listeners are commonly placed over respective entity classes such as,
@Entity
@EntityListeners(EntityListener.class)
public class Entity implements Serializable {
//...
}
Applications may use one or more class libraries for sharing common functionalities across different projects/modules. In addition to the EE module, class libraries basically also require entities to be present on the compile-time class-path as a dependency i.e entities are present in both the places namely a class library and an EE module in an enterprise application. As such, the class EntityListener
in the given example needs to be present on the compile-time class-path of a class library (it cannot only be added to the EE module).
If entity listeners were not to be tightly-coupled with respective entities and be specified separately then, there would be no need to add this dependency (listeners) to a class library i.e. entity listeners would then be present only in the EE project where EJBs are perfectly eligible for injection using @Inject
.
Is there any possibility to separate this annotation @EntityListeners(EntityListener.class)
from its associated entity so that it can be declared separately in a separate place? There should not be need to tightly couple this annotation with its respective entity.
Using GlassFish 4.1 having EclipseLink 2.6.0 (JPA 2.1).
This is required as there is a problem injecting EJBs into such entity listeners available in class libraries using the CDI specific artifact @Inject
. EJBs could otherwise be injected by using @Inject
into listeners, if the listeners were present in the EE project (module) only (but not in a class library).