I am trying to inject a @Stateless
EJB into my EntityListner. The @PrePresist
method of my EntityListener is invoked, but the injected EJB resolves to null
so a NullPointerException is thrown.
public class MyEntityListener {
@Inject
private MyService myService; // @Stateless EJB
@PrePersist
public void prePersist(MyEntity entity) {
final Foo foo = myService.getFoo(); // !! NPE thrown here!!
entity.setFoo(foo);
}
}
Previous questions which I found here on SO are dealing with the problem, that JPA does provide CDI only from JPA 2.1 on but not for JPA 2.0, but I am using JPA 2.1. So what else may be the problem here?
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
>
<persistence-unit name="MY_PU" transaction-type="JTA">
<jta-data-source>jdbc/local</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.logging.level" value="ALL" />
</properties>
</persistence-unit>
</persistence>
Injection of my EJB into other session beans works fine, both with EJB inection (@EJB
) as well as CDI injection (@Inject
). It only fails in the EntityListner.
I am using GlassFish 4.1 with EclipseLink 2.5.2
Related questions: Injecting a SessionScoped Stateful bean in EntityListener