users@glassfish.java.net

EntityManager NullPointerException with PersistenceContext

From: <forums_at_java.net>
Date: Tue, 20 Dec 2011 05:20:40 -0600 (CST)

Hi guys,

Another newbie here. Just starting out with JavaEE and Glasfish3. I am trying
to write a RESTful service which accepts a parameter (tracking) and searches
database to find the record/entity. I've worked with JPA before and I have it
implemented in my other projects. My problem is that I get a
NullPointerException on my EntityManager. I am following a book JavaEE with
Glashfish3 by Antonio Goncalves and I can't find the reason why it does that.
Couple of things to note:
DG Object is an @Entity
I cannot for some reason add @Stateles to OrderResource class. Says it cannot
find it, even thought javax.Persistence is added as a library.
I've never declared EntitryManager in this way. I've always created
EntityManagerFactor, but if I do it in this project I get an error that
Persistence provider can't be found even though eclipse api is added as a
library to the compile output.

This is the code and the bold line is where the exception is thrown:

import com.consoledoctor.dgeneral.DG;
import com.consoledoctor.objectmanager.OrderManager;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBElement;
import java.net.URI;
 

@Path("domestic")
@Produces({"application/xml", "application/json"})
@Consumes({"application/xml", "application/json"})
public class OrderResource {

    @PersistenceContext(unitName = "read_domestic_test")
    private EntityManager em;
    @Context
    private UriInfo uriInfo;
    private OrderManager om = new OrderManager();

    @POST
    public Response createOrder(JAXBElement<DG> dgJaxb) {
        om.loadOrder(dgJaxb.getValue());
        em.persist(om.getCurrentOrder());
        URI bookURI =
uriInfo.getAbsolutePathBuilder().path(om.getTracking()).build();
        return Response.created(bookURI).build();
    }

    @GET
    @Path("{tracking}/")
    public DG getOrderByTracking(@PathParam("tracking") String tracking) {
        try {
           * DG dg = em.find(DG.class, tracking);*
            dg.setTracking(tracking);
            return dg;
        } catch (NullPointerException npe) {
            npe.printStackTrace();
        }
        return null;
    }

 

And the persistence.xml:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="read_domestic_test"
 transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.consoledoctor.dgeneral.DG</class>
<class>com.consoledoctor.dgeneral.DGNoteV2</class>
<class>com.consoledoctor.GenOrder</class>
<properties>
<property name="javax.persistence.jdbc.url"
value="jdbc:mysql://server:port/db_name"/>
<property name="eclipselink.ddl-generation" value="none"/>
<property name="javax.persistence.jdbc.driver"
value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="dg_test"/>
<property name="javax.persistence.jdbc.password" value="domestic"/>
</properties><
/persistence-unit>

 

Any help would be appreciated.

 

Thanks, Have a good day.

 

 




 


--
[Message sent by forum member 'DMan']
View Post: http://forums.java.net/node/875615