PersistenceContext EntityManager 注入 NullPointerException [英] PersistenceContext EntityManager injection NullPointerException

查看:46
本文介绍了PersistenceContext EntityManager 注入 NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一场包含以下内容的战争:

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

持久性.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>

我的TestServlet类如下:

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();
    }
}

当调用 get() 方法时,我得到一个 NullPointerException;EntityManager 尚未注入.关于我可能遗漏什么或如何诊断它的任何建议?服务器日志中的内容很少.

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.

我确定我在没有 jboss-web.xml 或 web.xml 中的数据源条目的情况下进行了这项工作.我也将 ds.xml 单独部署到部署目录中,这肯定会被选中 - 我可以在 JMX 控制台中看到它.

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.

尝试使用 JBoss 4.2.3 和 6.0 构建,结果相同.

Tried using JBoss 4.2.3 and a 6.0 build with the same result.

推荐答案

实体管理器只能注入在事务内部运行的类中.换句话说,它只能注入到 EJB 中.其他类必须使用 EntityManagerFactory 来创建和销毁 EntityManager.

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.

由于您的 TestService 不是 EJB,注释 @PersistenceContext 被简单地忽略.不仅如此,在 JavaEE 5 中,不可能在 JAX-RS 服务中注入 EntityManager 或 EntityManagerFactory.您必须使用 JavaEE 6 服务器(JBoss 6、Glassfish 3 等).

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).

以下是注入 EntityManagerFactory 的示例:

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();
        }
    }
}

这里最简单的方法是将您的服务声明为 EJB 3.1,假设您使用的是 JavaEE 6 服务器.

The easiest way to go here is to declare your service as a EJB 3.1, assuming you're using a JavaEE 6 server.

相关问题:将 EJB 注入 JAX-RS(RESTful 服务)

这篇关于PersistenceContext EntityManager 注入 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆