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>
2
  • Could you add the code where the crash occurs and the full stack trace of the error? Commented Feb 11, 2012 at 8:19
  • @Traroth It happens on: this.entityManager.persist(employee), because entityManager is not injected and is null. Commented Feb 11, 2012 at 8:23

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

2 Comments

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?
Yes you can, for example here stackoverflow.com/questions/2421339/… 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.
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.

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.