lundi 4 mai 2015

Use of Inject at PostLoad listner throws Nullpointer exception

I am trying to inject other beans in my EnityListner class.

DB Service

@Stateless
@DependsOn("MongoDbConnector")
public class EmployeeDbService{
    private final transient Datastore ds;

    @Inject
    public EmployeeDbService(MongoDbConnector connector) {
        ds = connector.getDatastore();        
    }

    @Override
    public List<Employement> getAllPastEmployersById(Integer employeeId) {
        return ds.createQuery(Employement.class).filter("employeeId", employeeId).asList();
    } 
}

My Entity

@Entity("employees")
@EntityListeners(EmployeeWatcher.class)
public final class Employee extends BaseEntity implements Serializable {

    private static final long serialVersionUID = 1L;    
    private String name;
    private int employeeId;

    @Transient
    private List<Employement> employements = new ArrayList<Employement>();


    public Employee() {}

    public Employee(int employeeId) {
        this.employeeId = employeeId;
    }

    //getters and setters
}

@Entity(value = "employemnts", noClassnameStored = true)
public class Employement implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    private ObjectId id;
    //some more variables and getters ans setters
}

My Listner

@Stateless
public class EmployeeWatcher {
    private EmployeeDbService employeeDbService;

    @Inject
    public EmployeeWatcher(EmployeeDbService employeeDbService) {
        this.employeeDbService = employeeDbService;
    }

    public EmployeeWatcher() {}

    @PostLoad
    public void postPersist(Employee employee) {
        List<Employement> employements= employeeDbService.getAllPastEmployersById(employee.getEmployeeId());
        employements.forEach(employee::addEmployment);
    }
}

I am getting this nullpointer exception at EmployeeWatcher class that employeeDbService is null.

Caused by: javax.ejb.EJBException: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:190) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:275) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:340) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
    at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:239) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
    at org.jboss.as.ejb3.component.invocationmetrics.WaitTimeInterceptor.processInvocation(WaitTimeInterceptor.java:43) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) [jboss-invocation-1.2.1.Final.jar:1.2.1.Final]
------------------------------------------
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at org.mongodb.morphia.mapping.MappedClass.callLifecycleMethods(MappedClass.java:435) [morphia-0.111.jar:]
    at org.mongodb.morphia.mapping.Mapper.fromDb(Mapper.java:604) [morphia-0.111.jar:]
    at org.mongodb.morphia.mapping.Mapper.fromDBObject(Mapper.java:306) [morphia-0.111.jar:]
    at org.mongodb.morphia.query.MorphiaIterator.convertItem(MorphiaIterator.java:79) [morphia-0.111.jar:]
    at org.mongodb.morphia.query.MorphiaIterator.processItem(MorphiaIterator.java:65) [morphia-0.111.jar:]
    at org.mongodb.morphia.query.MorphiaIterator.next(MorphiaIterator.java:60) [morphia-0.111.jar:]
    at org.mongodb.morphia.query.QueryImpl.asList(QueryImpl.java:323) [morphia-0.111.jar:]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.8.0_40]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [rt.jar:1.8.0_40]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.8.0_40]
    at java.lang.reflect.Method.invoke(Method.java:497) [rt.jar:1.8.0_40]

Aucun commentaire:

Enregistrer un commentaire