Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

EntityManager is null in rest ws  RSS feed

 
Dilshan De Silva
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made small rest ws to learn rest. With out using EntityManager its run fine. But when i'm using EntityManager it'll give NPE. I'm using jboss-eap-6.2,EJB 3, JPA. After google that error find out EntityManager is null. How to solve this problem.

@Stateless
@Path("/dili")

public class TestResource {

@PersistenceContext(unitName = "AjaxrestPU")
private EntityManager em;

@GET
@Path("{key}")
@Produces("application/xml")
public Customer getMsg(@PathParam("key") int key) {
Customer cu = em.find(Customer.class, 1);
System.out.println("key " + key);

return cu;
}

}

persistance.xml

<persistence-unit name="AjaxrestPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/tutes</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
</properties>
</persistence-unit>
Error in boss

13:09:38,862 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default
host].[/Ajaxrest].[org.test.ApplicationConfig]] (http-/127.0.0.1:8080-2) JBWEB0
0236: Servlet.service() for servlet org.test.ApplicationConfig threw exception:
org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException
at org.jboss.resteasy.core.SynchronousDispatcher.handleApplicationExcep
ion(SynchronousDispatcher.java:365) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.
.7.Final-redhat-2]
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Few questions
a. Is persistence xml in correct place (folder) as per requirement from EJB container of JBoss ?
b. Is DS settings are correct in web.xml, jboss-web.xml ?

c. Most important one - How do you get a reference of the EJB in your Client code? Any JNDI lookup being used ? Can you post your client code where this lookup is happening? And also post the code where you are checking the injected persistence context.
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also do you have beans.xml file to enable CDI for your app ?
 
Dilshan De Silva
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhay Agarwal wrote:Also do you have beans.xml file to enable CDI for your app ?


Thanks. After creating empty beans.xml all works fine.
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
glad to know that my pointer helped you in solving your error
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!