kasavbere
kasavbere

Reputation: 6003

entityManager.persist(object) NullPointerException

I have been struggling all week to learn Java EE. I am building a REST api using Restlet 2.0, Spring, Hibernate, and Maven through Netbeans 7.2. Now I am at the point where whenever I make a service call that tries to persist data to the database, the following method throws a NullPointerException

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

From looking at the code, the following piece is supposed to inject the entityManager

protected EntityManager entityManager;

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

I found the link EntityManager injection results in NullPointerException. But I am too new to understand how to implement it in my code. Also, I am using Glassfish 3.1.2.

Will someone please help with some code sample?

EDIT/UPDATE:

Below is the persistence.xml file I am using. I am running MySQL and Glassfish. 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>

Upvotes: 0

Views: 9901

Answers (3)

Adam Wise
Adam Wise

Reputation: 2290

The above answers are useful. The very short version is that likely your EntityManager is null. The call itself shouldn't throw a null pointer exception.

Upvotes: 0

Arun Chandrasekaran
Arun Chandrasekaran

Reputation: 2579

Incase of Java EE 5, @PersistenceContext(unitName="your_unit_name") can be used only container managed objects like EJB or Servlet.

Incase you want to create EntityManager object in a non-container managed object like a plain java class use refer the following sample.

private EntityManagerFactory emf;

public static EntityManagerFactory getEntityManagerFactory() {
    if (emf == null) {
        emf = Persistence.createEntityManagerFactory("your_unit_name");
    }
    return emf;
}

public static EntityManager getEntityManager() {
    EntityManager entityManager = null;
    if (null != emf)
           entityManager = emf.createEntityManager();

    return entityManager;
}

Incase of Java EE 6, you may refer CDI - Context and Dependency injection on how to inject EntityManager.

Upvotes: 0

Ilya
Ilya

Reputation: 29703

setEntityManager 

instead of

setEngityManager  

you have typo.

Create persistence unit, like here
java.net

and inject EntityManager by persistence unit's name

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

Upvotes: 0

Related Questions