annotated entity manager null pointed exception

2019-07-10 18:57发布

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>

4条回答
Explosion°爆炸
2楼-- · 2019-07-10 19:33

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.

查看更多
对你真心纯属浪费
3楼-- · 2019-07-10 19:34

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.

查看更多
甜甜的少女心
4楼-- · 2019-07-10 19:38

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.

查看更多
来,给爷笑一个
5楼-- · 2019-07-10 19:51

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
查看更多
登录 后发表回答