Boa tarde, quando injeto o EntityManager pelo CDI, não funciona, um nullpointexception acontece, mas quando uso o new funciona normal.
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.persistence.EntityManager;
@FacesConverter("cargoConverter")
public class CargoConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String valor) {
Long id = Long.valueOf(valor);
EntityManager em = new EntityFactory().createEntityManager();
return em.find(Cargo.class, id);
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object object) {
if (object != null) {
return ((Cargo) object).getId().toString();
}
return "";
}
}
@ApplicationScoped
public class EntityFactory implements Serializable {
private static final long serialVersionUID = 1L;
@Produces
@RequestScoped
public EntityManager createEntityManager() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("projeto");
return emf.createEntityManager();
}
public void destroyEntityManager(@Disposes EntityManager entity) {
if (entity.isOpen()) {
entity.close();
}
}
}