2

I am doing a small project using JPA. I need to insert the employee object. For that when I use the annotated entity manager I got the NullPointer exception. But when I use the Normal EntityManager without using the annotation it is working fine. Do I need to configure somewhere else other than persistence.xml to work this examle fine?

Please see the code below.

public class EmployeeDao implements IEmployeeDao{       

     @PersistenceContext(unitName = "timesheet")
    private EntityManager entityManager ;

    @Override
    public boolean createEmployee(IEmployee employee) { 

        this.entityManager.persist(employee);       

        return true;
    }
}

persistence.xml

<?xml version="1.0" encoding="UTF-8" ?>
<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_2_0.xsd"
    version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="timesheet" transaction-type="RESOURCE_LOCAL">       
        <class>com.timesheet.model.Employee</class>     
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url"
                value="jdbc:mysql://localhost:3306/timesheet" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="" />

            <!-- EclipseLink should create the database schema automatically -->
            <property name="eclipselink.ddl-generation" value="create-tables" />
            <property name="eclipselink.ddl-generation.output-mode"
                value="database" />
        </properties>

    </persistence-unit>
</persistence>
user414967
  • 5,225
  • 10
  • 40
  • 61

4 Answers4

3

Injection of resources (in your case via use of @PersistenceContext) works only in container managed classes (like EJBs and Servlets). This explained with more details for example in Java EE specification v6, EE5.2.5.

What you can do:

  • Modify your class so that it is managed class
  • move injection of resources to managed class and pass it to EmployeeDao,
  • use JNDI lookup as before
Mikko Maunu
  • 41,366
  • 10
  • 132
  • 135
  • but here in this project I am not using any ejb or servlet. I will directly access the service. In that case, Can I configure spring to make this work? – user414967 Feb 11 '12 at 08:24
  • Yes you can, for example here http://stackoverflow.com/questions/2421339/how-to-inject-jpa-entitymanager-using-spring is explained how to without extending JpaDaoSupport. I would probably go for JNDI lookup, if Spring is not otherwise used in project. But that's just matter of opinion. – Mikko Maunu Feb 11 '12 at 08:30
1

Use the annotation javax.ejb.Stateless for your EmployeeDao and IEmployeeDao classes. Entitymanager is a no-interface an Enterprise Java Bean injected in your client.

The client must be either a web component or another enterpise bean. See here for further details about how to use EJBs.

In other words, using the Stateless annotation, the web container will take care of the lifecycle of your EmployeeDao class.

perissf
  • 15,979
  • 14
  • 80
  • 117
0

Use transaction type of JTA instead of RESOURCE_LOCAL. You can acquire the EntityManager instance if and only if the program is running in app server and transaction type is JTA.

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
0

So above all answers lead to summarise that :

  • Injection of resources through annotation will work on container managed classes (e.g EJB, Servlet).
  • Injection of resources can be done except annotation (e.g @PersistenceContext) on a simple JPA project ( a project which hasn't container managed classes ). Following code snippet give you lucid view :

      private EntityManagerFactory factory;
      factory = Persistence.createEntityManagerFactory("PERSISTENCE_UNIT_NAME");
      EntityManager em = factory.createEntityManager();
    

Thanks to all for sharing knowledge.

atiqkhaled
  • 386
  • 4
  • 19