PersistenceContext EntityManager注入NullPointerException [英] PersistenceContext EntityManager injection NullPointerException
问题描述
我有一场包含以下内容的战争:
META-INF / MANIFEST.MF
WEB-INF / classes / META-INF / persistence.xml
WEB-INF / classes / com / test / service / TestServlet.class
WEB-INF / classes / com / test / service / TestEntity.class
WEB-INF / classes / jndi.properties
WEB-INF / classes / postgresql-ds.xml
WEB-INF / jboss-web.xml
WEB-INF / web.xml
index.jsp
persistence.xml:
<?xml version =1.0encoding =UTF-8?>
< persistence xmlns =http://java.sun.com/xml/ns/persistencexmlns: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_1_0.xsdversion =1.0>
< persistence-unit name =test>
< provider> org.hibernate.ejb.HibernatePersistence< / provider>
< jta-data-source> java:/ TestDS< / jta-data-source>
<属性>
< property name =hibernate.dialectvalue =org.hibernate.dialect.PostgreSQLDialect/>
< property name =hibernate.hbm2ddl.autovalue =update/>
< property name =hibernate.show_sqlvalue =true/>
< / properties>
< / persistence-unit>
< /余辉>
web.xml:
<!DOCTYPE web-app PUBLIC
- // Sun Microsystems,Inc. //DTD Web Application 2.3 // EN
http://java.sun .com / dtd / web-app_2_3.dtd>
< web-app>
< display-name>测试Web应用程序< / display-name>
< context-param>
< param-name> resteasy.scan< / param-name>
< param-value> true< /参数值>
< / context-param>
< listener>
< listener-class> org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap< / listener-class>
< / listener>
< servlet>
< servlet-name> Resteasy< / servlet-name>
< servlet-class> org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher< / servlet-class>
< / servlet>
< servlet-mapping>
< servlet-name> Resteasy< / servlet-name>
< url-pattern> / service / *< / url-pattern>
< / servlet-mapping>
< resource-ref>
< res-ref-name> TestDS< / res-ref-name>
< res-type> javax.sql.DataSource< / res-type>
< res-auth>容器< / res-auth>
< res-sharing-scope>可分享< / res-sharing-scope>
< / resource-ref>
< / web-app>
我的TestServlet类如下所示:
package com.test.service;
import java.util。*;
import javax.persistence。*;
import javax.ws.rs。*;
@Path(/ service)
public class TestService {
@PersistenceContext(unitName =test)
private EntityManager em;
@GET
@Path(/ get)
@Produces(application / json)
public List get(){
return em.createQuery(来自TestEntity)。getResultList();
}
}
当get()方法被调用时,我得到一个NullPointerException; EntityManager尚未注入。关于我可能会丢失什么或我如何诊断它的任何建议?在服务器日志中很少。
我确信没有jboss-web.xml或web.xml中的数据源条目,我可以正常工作。我已经将ds.xml单独部署到了deploy目录中,这一定会被拿出来 - 我可以在JMX控制台中看到它。
使用JBoss 4.2.3和一个6.0 build有相同的结果。
实体管理器只能在事务内部运行的类中注入。换句话说,它只能被注入到EJB中。其他类必须使用EntityManagerFactory来创建和销毁EntityManager。
由于您的TestService不是EJB,因此注释@PersistenceContext将被忽略。不仅如此,在JavaEE 5中,不可能在JAX-RS服务中注入EntityManager和EntityManagerFactory。您必须使用JavaEE 6服务器(JBoss 6,Glassfish 3等)。
以下是一个注入EntityManagerFactory的示例:
package com.test.service;
import java.util。*;
import javax.persistence。*;
import javax.ws.rs。*;
@Path(/ service)
public class TestService {
@PersistenceUnit(unitName =test)
private EntityManagerFactory entityManagerFactory;
@GET
@Path(/ get)
@Produces(application / json)
public List get(){
EntityManager entityManager = entityManagerFactory.createEntityManager();
try {
return entityManager.createQuery(from TestEntity)。getResultList();
} finally {
entityManager.close();
}
}
}
最简单的方法假设您使用的是JavaEE 6服务器,则将其声明为EJB 3.1。
相关问题:
将EJB注入JAX-RS(RESTful服务)
I have a war containing the following:
META-INF/MANIFEST.MF
WEB-INF/classes/META-INF/persistence.xml
WEB-INF/classes/com/test/service/TestServlet.class
WEB-INF/classes/com/test/service/TestEntity.class
WEB-INF/classes/jndi.properties
WEB-INF/classes/postgresql-ds.xml
WEB-INF/jboss-web.xml
WEB-INF/web.xml
index.jsp
persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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_1_0.xsd" version="1.0">
<persistence-unit name="test">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/TestDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
web.xml:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Test Web Application</display-name>
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<resource-ref>
<res-ref-name>TestDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</web-app>
My TestServlet class is as follows:
package com.test.service;
import java.util.*;
import javax.persistence.*;
import javax.ws.rs.*;
@Path("/service")
public class TestService {
@PersistenceContext(unitName = "test")
private EntityManager em;
@GET
@Path("/get")
@Produces("application/json")
public List get() {
return em.createQuery("from TestEntity").getResultList();
}
}
When the get() method is invoked I get a NullPointerException; the EntityManager hasn't been injected. Any suggestions on what I might be missing or how I can diagnose it? There's very little in the server log.
I'm sure I had this working without the jboss-web.xml or the datasource entry in web.xml. I've deployed the ds.xml to the deploy directory separately too and that's definitely picked up - I can see it in the JMX console.
Tried using JBoss 4.2.3 and a 6.0 build with the same result.
An entity manager can only be injected in classes running inside a transaction. In other words, it can only be injected in a EJB. Other classe must use an EntityManagerFactory to create and destroy an EntityManager.
Since your TestService is not an EJB, the annotation @PersistenceContext is simply ignored. Not only that, in JavaEE 5, it's not possible to inject an EntityManager nor an EntityManagerFactory in a JAX-RS Service. You have to go with a JavaEE 6 server (JBoss 6, Glassfish 3, etc).
Here's an example of injecting an EntityManagerFactory:
package com.test.service;
import java.util.*;
import javax.persistence.*;
import javax.ws.rs.*;
@Path("/service")
public class TestService {
@PersistenceUnit(unitName = "test")
private EntityManagerFactory entityManagerFactory;
@GET
@Path("/get")
@Produces("application/json")
public List get() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
try {
return entityManager.createQuery("from TestEntity").getResultList();
} finally {
entityManager.close();
}
}
}
The easiest way to go here is to declare your service as a EJB 3.1, assuming you're using a JavaEE 6 server.
Related question: Inject an EJB into JAX-RS (RESTful service)
这篇关于PersistenceContext EntityManager注入NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!