我正在尝试创建一个DataManager,用作我的Beans的外观。 不幸的是,我无法让EntityManager工作。 我试图通过@PersistenceContext注入它,但是当使用EntityManager时,我总是得到一个NullPointerException。
@ApplicationScoped
public class DataManager {
@PersistenceContext(unitName = "MSM")
private EntityManager em;
/*
* Adds a new employee to the database.
* @param employee - the employee object that will be added.
* @return true if transaction was successful.
*/
public boolean createEmployee(Employee employee){
try {
if (employee != null) {
if (em.createNamedQuery("Employee.findByMail").setParameter("mail",employee.getMail()).getResultList().isEmpty()) {
em.persist(employee);
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}