JPAを使用して小さなプロジェクトを行っています。私は従業員オブジェクトを挿入する必要があります。 注釈付きエンティティマネージャを使用すると、NullPointer例外が発生します。 しかし、注釈を使用せずに通常のEntityManagerを使用するとうまくいきます。 この罰金を働かせるためにpersistence.xml以外のどこかを設定する必要がありますか?アノテートされたエンティティマネージャヌル先取り例外
下記のコードをご覧ください。
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>
クラッシュが発生するコードとエラーの完全なスタックトレースを追加できますか? –
@Traroth entityManagerが挿入されておらず、nullであるため、this.entityManager.persist(employee)で発生します。 –