Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PersistenceContext EntityManager injection 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

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.

like image 213
rich Avatar asked Jan 16 '11 21:01

rich


2 Answers

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)

like image 148
Andre Rodrigues Avatar answered Sep 21 '22 11:09

Andre Rodrigues


If the component is an EJB, then, there shouldn't be a problem injecting an EM.

But....In JBoss 5, the JAX-RS integration isn't great. If you have an EJB, you cannot use scanning and you must manually list in the context-param resteasy.jndi.resource. If you still have scanning on, Resteasy will scan for the resource class and register it as a vanilla JAX-RS service and handle the lifecycle.

This is probably the problem.

like image 45
Bill Burke Avatar answered Sep 23 '22 11:09

Bill Burke