问题
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>
回答1:
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
回答2:
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.
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/9238696/annotated-entity-manager-null-pointed-exception