问题描述
我做使用JPA一个小项目。我需要插入Employee对象。
对于我使用注释实体管理器时,我得到了空指针异常。
但是,当我用普通的EntityManager不使用它工作正常的注释。
我是否需要配置其他地方其他比的persistence.xml来解决这个〔实施例罚款?
请参阅下面的code。
公共类EmployeeDao实现IEmployeeDao { @PersistenceContext(的unitName =时间表)
私人EntityManager的EntityManager的; @覆盖
公共布尔在CreateEmployee(IEmployee员工){ this.entityManager.persist(员工); 返回true;
}
}
的persistence.xml
<?XML版本=1.0编码=UTF-8&GT?;
<持久性的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
版本=2.0的xmlns =http://java.sun.com/xml/ns/persistence>
<持久性单位名称=时间表交易型=RESOURCE_LOCAL>
<类别和GT; com.timesheet.model.Employee< /班>
<性状>
<属性名=javax.persistence.jdbc.driverVALUE =com.mysql.jdbc.Driver/>
<属性名=javax.persistence.jdbc.url
值=的jdbc:mysql的://本地主机:3306 /时间表/>
<属性名=javax.persistence.jdbc.userVALUE =根/>
<属性名=javax.persistence.jdbc.passwordVALUE =/> <! - 的EclipseLink应该自动创建数据库模式 - >
<属性名=eclipselink.ddl代值=创建的表/>
<属性名=eclipselink.ddl-generation.output模式
值=数据库/>
< /性状> < /持久化部>
< /持久>
资源注入(在通过使用你的情况的 @PersistenceContext 的),只能在容器管理类(比如EJB和Servlet)。这与Java EE规范的V6更详细的信息为例进行说明,EE5.2.5。
你可以做什么:
- 修改类使其托管类
- 资源的举动注射托管类,并把它传递给
EmployeeDao, - 使用JNDI查找和以前一样
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>
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 toEmployeeDao,
- use JNDI lookup as before
这篇关于注释实体管理器空尖例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!