In your code sample, your CustomerJpaController is never instantiated. So, you get a null pointer exception.
I advise you to switch to CDI and rely on its injection method to have your entity manager (factory?) properly instantiated and injected in your controller when this last is instantiated. And, so, to use @Named instead of @ManagedBean.
So, you would have :
@Named
@RequestScoped
public class CustomerJpaController implements Serializable {
...
}
(or whichever scope better fits your need)
It seems to me that you should use an EntityManager (EM) rather than an EntityManagerFactory (EMF) in your controller.
If your EMF is container managed and you have only one persistence unit, you can use the standard JPA @PersistenceContext
annotation :
@PersistenceContext
private EntityManager entityManager;
If your EMF is not managed, you can leverage the power of deltaspike JPA module (remember : deltaspike is good for you :-) ) and inject an EntityManager in your controller :
@Named
@RequestScoped
public class CustomerJpaController implements Serializable {
@Inject
private EntityManager em;
}
This requires the implementation of an EntityManagerProducer class, which can have any name but must have one method annotated @Produces @RequestScoped
returning an EntityManager and another one taking an EntityManager parameter annotated with @Disposes. Ex :
public class MyEntityManagerProducer {
@Inject
@PersistenceUnitName("myPU")
private EntityManagerFactory emf;
@Produces
@RequestScoped
public EntityManager createEntityManager() {
return emf.createEntityManager();
}
public void disposeEntityManager(@Disposes em) {
if (em.isOpen()) {
em.close();
}
}
Note the usage of @PersistenceUnitName("myPU"), the deltaspike annotation that will handle the instanciation of the EMF.
If you have multiple persistence units, as it is often the case in the real world, you can set them apart with qualifiers. To declare a qualifier, declare an @interface
with the following annotations :
@Target({ FIELD, METHOD, PARAMETER, TYPE })
@Retention(RUNTIME)
@Documented
@Qualifier
public @interface MyQualifier {
}
Then, add this qualifier to all @Produces, @Disposes and @Inject, to allow CDI to decide which persistence unit / entity manager you are willing to use :
public class MyEntityManagerProducer {
@Inject
@PersistenceUnitName("myPU")
private EntityManagerFactory emf;
@Produces
@MyQualifier
@RequestScoped
public EntityManager createEntityManager() {
return emf.createEntityManager();
}
public void disposeEntityManager(@Disposes @MyQualifier em) {
if (em.isOpen()) {
em.close();
}
}
and in your controller :
@Named
@RequestScoped
public class CustomerJpaController implements Serializable {
@Inject
@MyQualifier
private EntityManager em;
}
All this requires CDI. Configuring CDI is way beyond a short answer to your question. I use OpenWebBeans in all my projects. Weld is also very popular.