2

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

Community
  • 1
  • 1
stg
  • 2,757
  • 2
  • 28
  • 55
  • What properties are you using in your persistence unit? – Chris Sep 21 '15 at 13:38
  • @Chris There is nothing special in there. I updated my quesiton and added the missing part of the persitence.xml – stg Sep 21 '15 at 13:42

1 Answers1

0

There is nothing wrong in the code, the behaviour is caused by this bug:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=438105

The bug is fixed in EclipseLink 2.6.0. An update to this version solved my problem.

http://www.eclipse.org/eclipselink/downloads/

stg
  • 2,757
  • 2
  • 28
  • 55