Home Can't inject EntityManager via @PersistenceContext - NullPointerException
Reply: 0

Can't inject EntityManager via @PersistenceContext - NullPointerException

Fynn
1#
Fynn Published in 2017-12-05 14:13:38Z

I am trying to create a DataManager, that is used as a facade for my Beans. Unfortunately I can't get the EntityManager to work. I am trying to inject it via @PersistenceContext, but when using the EntityManager I always get a 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;
  }

}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.299611 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO