我正在使用Tomcat,Weld(用于CDI)和DeltaSpike(数据,JPA和JSF模块)设置新项目
我现在已经达到了这一点,我正在尝试按照DeltaSpike文档(http://deltaspike.apache.org/documentation/data.html)中的说明创建JPA存储库(由Hibernate支持)。
我正在努力以文档描述的方式连接持久性单元,因为我的EntityManagerFactory对象始终为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();
}
}
}
我创建了一个persistence.xml文件,该文件位于包含我的服务/数据层的jar文件的/ META-INF /目录中
<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>
我之前使用Spring处理依赖注入和JPA的工作正常,因此我知道我的数据源已正确设置为Tomcat JNDI资源。
现在我看到了这个异常(暗示没有注入EntityManagerFactory)
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)
我怀疑在设置持久性单元时我遗漏了一些东西。
我还需要做些什么来创建一个由链接到我的数据源的hibernate jpa支持的有效持久性单元?