2

I am setting up a new project using Tomcat, Weld (for CDI) and DeltaSpike (Data, JPA and JSF modules)

I have reached the point now where I am trying to create a JPA repository (backed by Hibernate) by following the directions in the DeltaSpike documentation (http://deltaspike.apache.org/documentation/data.html).

I am struggling to hook up the persistence unit in the way the documentation describes as my EntityManagerFactory object is always null

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;

public class EntityManagerProducer {

    @PersistenceUnit(unitName = "srdPersistenceUnit")
    private EntityManagerFactory emf;

    @Produces // you can also make this @RequestScoped
    public EntityManager create() {
        return emf.createEntityManager();
    }

    public void close(@Disposes EntityManager em) {
        if (em.isOpen()) {
            em.close();
        }
    }
}

I have created a persistence.xml file which is in the /META-INF/ directory of the jar file that contains my service/data layer

<persistence version="2.0"
             xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="
        http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="srdPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <non-jta-data-source>java:comp/env/jdbc/srdDCP</non-jta-data-source>
        <properties>
        </properties>
    </persistence-unit>
</persistence>

I was previously using Spring to handle dependency injection and the JPA stuff which worked fine so I know my data source is set up correctly as a Tomcat JNDI resource.

Now however I see this exception (suggesting that the EntityManagerFactory is not being injected)

Caused by: java.lang.NullPointerException
    at testproj.data.entity.EntityManagerProducer.create(EntityManagerProducer.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.jboss.weld.injection.StaticMethodInjectionPoint.invoke(StaticMethodInjectionPoint.java:88)
    at org.jboss.weld.injection.StaticMethodInjectionPoint.invoke(StaticMethodInjectionPoint.java:78)
    at org.jboss.weld.injection.producer.ProducerMethodProducer.produce(ProducerMethodProducer.java:95)
    at org.jboss.weld.injection.producer.AbstractMemberProducer.produce(AbstractMemberProducer.java:167)
    at org.jboss.weld.bean.AbstractProducerBean.create(AbstractProducerBean.java:183)
    at org.jboss.weld.context.unbound.DependentContextImpl.get(DependentContextImpl.java:69)
    at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy.java:101)
    at org.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:50)
    at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:761)
    at org.jboss.weld.bean.builtin.InstanceImpl.getBeanInstance(InstanceImpl.java:179)
    at org.jboss.weld.bean.builtin.InstanceImpl.get(InstanceImpl.java:99)
    at org.apache.deltaspike.data.impl.handler.EntityManagerLookup.lookupFor(EntityManagerLookup.java:58)
    at org.apache.deltaspike.data.impl.handler.QueryHandler.createContext(QueryHandler.java:104)
    at org.apache.deltaspike.data.impl.handler.QueryHandler.invoke(QueryHandler.java:77)

I suspect I have missed something in the setting up of the persistence unit.

What more do I need to do to create a valid persistence unit backed by hibernate jpa linked to my datasource?

0 Answers0