EntityManager NullPointerException

时间:2014-06-06 16:15:07

标签: java-ee jpa ejb glassfish-3

另一个'EntityManager为空'问题。我找了一个答案,但没找到一个适合我的答案。我正在接管一个大型项目,我正在尝试简化它以在调试时剪掉红色鲱鱼。我是一个简单的客户/订单系统,我只是尝试部署它并查看客户列表。基于我提出的另一个问题和resulting suggestion我对下面的内容(实际问题和底部的堆栈跟踪):

的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<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="my_PU" transaction-type="JTA">
      <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>my_datasource</jta-data-source>
        <class>my.database.model.Customer</class>
        <class>my.database.model.Order</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
    </persistence-unit>
</persistence>

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>MyWebApp</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
  <context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
  </context-param>
  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>
  <context-param>
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
    <param-value>/WEB-INF/balusc.taglib.xml</param-value>
  </context-param>
  <context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
  </context-param>
  <filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
  </filter-mapping>
  <context-param>
    <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
    <param-value>false</param-value>
  </context-param>
  <listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
  </listener>
</web-app>

网页 - menu.xhtml

<html
 xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">
<head>
<link rel="stylesheet" type="text/css" media="screen,projection"
    href="../css/menu.css" /></head>
<ui:component>
    <div class="menu-container">
        <div class="menu-tab">
            <ul>
                <li><a href="index.xhtml"><span>Home</span></a></li>

                <li><h:outputLink
                        value="#{customerController.getUrl()}">
                        <span><h:outputText value="Customer" /></span>
                    </h:outputLink></li>
                <li><a href=""><span>Logout</span></a></li>
            </ul>
        </div>
    </div>
</ui:component>
</html>

CustomerController.java

package my.webapp.web.controller;

import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import my.model.Customer;

@Named(value = "customerController")
@RequestScoped
public class CustomerController// extends FormRequestController
{
    private Customer customer;
    private String id;
    protected List<?> entityObjectList;
    protected boolean editMode;

    @PostConstruct
    public void init() {
        System.out.println("CustomerController init");
        setEntityObjectList(findAll());
    }

    public List<?> findAll() {
        List<Customer> dataList = Customer.getAllRecords();
        return dataList;

    }

    public String getUrl()
    {
        System.out.println("CustomerController getUrl");
        return "www.google.com";
    }
    //getters and setters ommitted for brevity
}

Customer.java

package my.model;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.ejb.EJB;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import my.database.manager.PersistenceStoreManager;

@Entity
@Table(name=Customer.TBL_NAME)
@NamedQueries({
    @NamedQuery(name=Customer.QRY_ALL, query="select object(a) from Customer a")
})
public class Customer implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * Manager class to manager DB connections
     */
    @EJB
    private static PersistenceStoreManager<Customer> psm;
    // Table constants
    public static final String TBL_NAME = "Customer";
    public static final String QRY_ALL = TBL_NAME + ".all";

    private int id;
    private String name;
    private Set<Order> orders;

    public Customer() {
    }


    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public int getId() {
        return this.id;
    }

    //bi-directional many-to-one association to Order
    @OneToMany(mappedBy="id")
    public Set<Order> getOrders() {
        return this.orders;
    }

    public void setOrders(Set<Order> orders) {
        this.orders = orders;
    }

    public static List<Customer> getAllRecords(){
        Map<String, Object> params = new HashMap<String, Object>();
        return psm.getResultSetList(QRY_ALL, params);
    }
    //other getters and setters omitted for brevity
}

PersistenceStoreManager.java

package my.database.manager;

import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * This class is responsible for performing CRUD operations using the
 * EntityManager of the PersistenceContext.
 *
 *
 */
@Stateless
public class PersistenceStoreManager<T>
{
     @PersistenceContext(unitName = "my_PU")
    private EntityManager em;


    public PersistenceStoreManager ()
    {
        System.out.println("PersistenceStoreManager loaded");
        System.out.println("em is null: "+(em==null));
    }

    @PostConstruct
    public void postInject()
    {
        System.out.println("postInject");
        System.out.println("em is null: "+(em==null));
    }

    /**
     * @param namedQuery
     * @param criteria
     * @return
     */
    public List<T> getResultSetList(String namedQuery,
            Map<String, Object> criteria) {

        Query q = em.createNamedQuery(namedQuery);
        for (Map.Entry<String, Object> entry : criteria.entrySet()) {
            q.setParameter(entry.getKey(), entry.getValue());
        }
        List<T> resultSetList = q.getResultList();
        return resultSetList;
    }
}

我的应用程序部署但在尝试查看NullPointerException页面时我得到了无处不在的menu.xhtml。部署应用程序时会记录以下内容:

  

-WARNING | org.apache.catalina.connector.Request | _ThreadID = 37; _ThreadName = Thread-2; | PWC4011:无法从上下文将请求字符编码设置为UTF-8,因为已经读取了请求参数,或ServletRequest.getReader()已被称为|#]
  -info | uploadFileName = MyWebApp-0.0.1-SNAPSHOT.war |#]
  -INFO | javax.enterprise.system.container.ejb.com.sun.ejb.containers | EJB PersistenceStoreManager的可移植JNDI名称:[java:global / MyWebApp-0.0.1-SNAPSHOT / PersistenceStoreManager,java:global / MyWebApp-0.0 .1快照/ PersistenceStoreManager my.database.manager.PersistenceStoreManager] |!#]
  -INFO | javax.enterprise.system.std.com.sun.enterprise.server.logging | 加载PersistenceStoreManager |#]
  -INFO | javax.enterprise.system.std.com.sun.enterprise.server.logging | em为null:true |#]
  -INFO | javax.enterprise.resource.webcontainer.jsf.config |为上下文'/MyWebApp-0.0.1-SNAPSHOT'|#初始化Mojarra 2.1.6(SNAPSHOT 20111206)
  -INFO | org.primefaces.webapp.PostConstructApplicationEventListener |在PrimeFaces上运行3.4.1 |#]
  -INFO | javax.enterprise.system.container.web.com.sun.enterprise.web | WEB0671:在[/MyWebApp-0.0.1-SNAPSH 6243 OT]加载应用程序[MyWebApp-0.0.1-SNAPSHOT] |#]
  -INFO | javax.enterprise.system.tools.admin.org.glassfish.deployment.admin | MyWebApp-0.0.1-SNAPSHOT已在1,598毫秒内成功部署。|#]

当我导航到myserver时:8080 / MyWebApp-0.0.1-SNAPSHOT / faces / index.xhtml记录以下内容:

  

[#| 2014-06-06T16:51:56.988 + 0100 | SEVERE | glassfish3.1.2 | javax.enterprise.resource.webcontainer.jsf.application | _ThreadID = 36; _TreadName = Thread-2; |错误渲染视图[/index.xhtml]
  javax.el.E​​LException:/templates/menu.xhtml @ 15,64 value =“#{customerController.getUrl()}”:org.jboss.weld.exceptions.WeldException:WELD-000049无法调用[方法] @PostConstruct my.webapp.web.controller.CustomerController@4085f7ff上的public my.webapp.web.controller.CustomerController.init()    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114)
   在javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
   ... [长堆栈跟踪] ...
  引起:java.lang.NullPointerException
   at my.model.Customer.getAllRecords(Customer.java:64)
   at my.webapp.web.controller.CustomerController.findAll(CustomerController.java:41)
   at my.webapp.web.controller.CustomerController.init(CustomerController.java:37)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   ... [长堆栈跟踪] ......

正在输出postInject()CustomerCustomerController中的PersistenceStoreManager方法中的任何记录。由于它们使用@PostConstruct注释并且应该在注入后调用,因此我假设没有进行EJB注入。

有谁能看出为什么没有发生EJB注入?

1 个答案:

答案 0 :(得分:1)

不会注入EJB,因为它们只能注入容器托管类,而实体不是容器托管类。容器托管类列在例如Java EE 6 specification