Search code examples
hibernatejpajakarta-eeejbwildfly

Trouble persisting in an EJB project


I'm really needing some help here: I'm making my very first EJB project (3.2) with JPA 2.2 and in a WildFly application server with MySQL database. I'm trying to persist data using my EJB bean but getting a really huge and "says-nothing-to-me" error when the method is executed (called from a managed bean). I really have NO IDEA of what may be causing it (I thought it might have something to do with JTA but I'm not sure - really newbie in all these stuff).

I'm also not sure if this design is a good idea: I'm not using a DAO object in this example and I'm not sure about how should I be using it.

Do you guys have any idea of where should I start looking for the problem?

This is my Managed Bean. Nothing special here:

@ManagedBean
@ViewScoped
public class TesteMB {

    @Inject private TesteORM testeORM;

    @EJB private BaseEJBLocal baseService;

    public void gravar() {
        this.baseService.adiciona(this.testeORM);
    }

    public TesteORM getTesteORM() {
        return testeORM;
    }

    public void setTesteORM(TesteORM testeORM) {
        this.testeORM = testeORM;
    }

}

This is my EJB bean class (I removed the implementation of the interface because I read somewhere that it's not necessary in EJB 3.2... is that right?)

@Stateless
public class BaseEJBBean {

    @PersistenceContext
    protected EntityManager em;

    public void add(TestORM orm) {
        this.em.persist(orm);
    }
}

Here is my DAO class. I noticed that when I debug the code, it doesn't seem that it's passing through the EJB class (I put a breakpoint there and it goes straight to another breakpoint in the DAO class even not calling it anywhere... why is that?). I'm getting a null pointer in the second line of the constructor:

public class BaseDAO<T extends Entidade> {

    private final EntityManagerFactory factory;
    private final EntityManager em;
    private Class<T> clazz;

    public BaseDAO() {
        this.factory = Persistence.createEntityManagerFactory("caixaeestoque");
        this.em = factory.createEntityManager();
    }

    public void insert(T orm) {
        em.persist(orm);
    }

}

Here is the root cause of my exception:

Caused by: java.lang.NullPointerException at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:76) at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:118) at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1602) at org.hibernate.jpa.spi.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:210) at org.hibernate.jpa.internal.EntityManagerImpl.(EntityManagerImpl.java:91) at org.hibernate.jpa.internal.EntityManagerFactoryImpl.internalCreateEntityManager(EntityManagerFactoryImpl.java:345) at org.hibernate.jpa.internal.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:313) at br.com.meisa.caixaeestoque.comum.BaseDAO.(BaseDAO.java:15) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:408) at org.jboss.weld.injection.ConstructorInjectionPoint.newInstance(ConstructorInjectionPoint.java:113) at org.jboss.weld.injection.ConstructorInjectionPoint.invokeAroundConstructCallbacks(ConstructorInjectionPoint.java:87) at org.jboss.weld.injection.ConstructorInjectionPoint.newInstance(ConstructorInjectionPoint.java:74) at org.jboss.weld.injection.producer.AbstractInstantiator.newInstance(AbstractInstantiator.java:28) at org.jboss.weld.injection.producer.BasicInjectionTarget.produce(BasicInjectionTarget.java:116) at org.jboss.weld.injection.producer.BeanInjectionTarget.produce(BeanInjectionTarget.java:194) at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:149) at org.jboss.weld.context.unbound.DependentContextImpl.get(DependentContextImpl.java:69) at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:742) at org.jboss.weld.manager.BeanManagerImpl.getInjectableReference(BeanManagerImpl.java:840) at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:92) at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:370) at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:381) at org.jboss.weld.injection.producer.DefaultInjector$1.proceed(DefaultInjector.java:71) at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:48) at org.jboss.weld.injection.producer.DefaultInjector.inject(DefaultInjector.java:73) at org.jboss.weld.injection.producer.StatelessSessionBeanInjector.inject(StatelessSessionBeanInjector.java:58) at org.jboss.weld.injection.producer.ejb.SessionBeanInjectionTarget.inject(SessionBeanInjectionTarget.java:140) at org.jboss.as.weld.injection.WeldInjectionContext.inject(WeldInjectionContext.java:39) at org.jboss.as.weld.injection.WeldInjectionInterceptor.processInvocation(WeldInjectionInterceptor.java:51) at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) at org.jboss.as.ee.component.ManagedReferenceFieldInjectionInterceptorFactory$ManagedReferenceFieldInjectionInterceptor.processInvocation(ManagedReferenceFieldInjectionInterceptorFactory.java:109) at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) at org.jboss.as.ee.component.ManagedReferenceFieldInjectionInterceptorFactory$ManagedReferenceFieldInjectionInterceptor.processInvocation(ManagedReferenceFieldInjectionInterceptorFactory.java:109) at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53) at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) at org.jboss.as.ee.component.AroundConstructInterceptorFactory$1.processInvocation(AroundConstructInterceptorFactory.java:28) at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) at org.jboss.as.weld.injection.WeldInterceptorInjectionInterceptor.processInvocation(WeldInterceptorInjectionInterceptor.java:56) at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) at org.jboss.as.weld.ejb.Jsr299BindingsCreateInterceptor.processInvocation(Jsr299BindingsCreateInterceptor.java:94) at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50) at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309) at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:273) ... 101 more


Solution

  • I took a little long to realize that since I was using JTA I'm not allowed to instantiate a new Entity Manager (yeah, I know: newbie here).

    So I removed the whole constructor in BaseDAO class and simply annotated it with @PersistenceContext:

    private final static String PERSISTENCE_UNIT_NAME = "caixaeestoque";
    
    @PersistenceContext(unitName= PERSISTENCE_UNIT_NAME)
    private EntityManager em;
    

    Hope it helps someone else who's starting in EJB and full applications just like I was when I posted this question!