This discussion is archived
3 Replies Latest reply: Sep 30, 2012 1:59 AM by gimbal2 RSS

entityManager NullPointerException

964392 Newbie
Currently Being Moderated
I am using netbeans 7.2, Glassfish, MySQL, hibernate, Spring, and J2EE 6. I am also using Restlet 2.0.

I have a program that takes data from a web service call and persist it to the database. The important code samples are

protected EntityManager entityManager;

@PersistenceContext
public void setEntityManager(EntityManager entityManger) {
this.entityManager = entityManager;
}

@Override
public void save(T object) {
entityManager.persist(object);
}

The program throws a NullPointerException at the line entityManager.persist(object);

Below is the persistence.xml file I am using. Should I save it under WEB-INF which is where persistence-context.xml is OR under src/main/resource which is where hibernate.cfg.xml is?

<?xml version="1.0" encoding="UTF-8"?>
<persistence-unit name="pu1" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.mysite.restapi.RestletChildApplication</class>
<properties>
<property name="hibernate.connection.driver_class"
value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.url"
value="jdbc:mysql://localhost:3306/mydatabase" />
<property name="hibernate.connection.username" value="myname"/>
<property name="hibernate.connection.password" value="mypassword"/>
<property name="show_sql" value="true" />
<property name="dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>

Edited by: 961389 on Sep 28, 2012 5:50 PM
  • 1. Re: entityManager NullPointerException
    965123 Newbie
    Currently Being Moderated
    You cannot use @PersistenceContext in arbitrary classes. You can only use it in classes used for servlets, enterprise beans, JSF managed beans (and probably web service end points as well). Life cycle of such objects are managed by the container - which allows container to inject the EntityManager.

    Location of persistence.xml: WEB-INF/classes/META-INF/persistence.xml (In an IDE, create META-INF folder in the source directory, put the xml file there; it will automatically go to the correct location during compilation.

    In the xml, if you use JTA, you should use a jta data source configured in the application server. You don't specify db connection details in this xml.

    Also if you use JTA, you should be using the EntityManager inside enterprise beans. Or else to start and commit transactions, you should use an injected UserTransaction object and explicitly start and commit transactions inside methods like save().

    Are you using enterprise beans or is this just a servlet application?
  • 2. Re: entityManager NullPointerException
    964392 Newbie
    Currently Being Moderated
    Thanks for helping. Do you have an example persistence.xml I may see? I am using mysql and glassfish. I have configure a DataSource in glassfish called MySQL.
  • 3. Re: entityManager NullPointerException
    gimbal2 Guru
    Currently Being Moderated
    961389 wrote:
    Thanks for helping. Do you have an example persistence.xml I may see?
    Google can point you to a great number of them.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points