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

Can't inject EntityManager via @PersistenceContext - NullPointerException

user983 Published in July 17, 2018, 3:27 pm

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.

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()) {
                return true;
    } catch (Exception e) {
    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.404352 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO