问题
i am really noob in java ee
i want to make a query to verify if email is unique
after i search on web i mad i came to conclusion that me EntityManeger is null
this is my entity Customer and is an query Customer.findByEmail that is my point of focus
@Entity
@Table(name = "customer")
@NamedQueries({
@NamedQuery(name = "Customer.findAll", query = "SELECT c FROM Customer c"),
@NamedQuery(name = "Customer.findById", query = "SELECT c FROM Customer c WHERE c.id = :id"),
@NamedQuery(name = "Customer.findByName", query = "SELECT c FROM Customer c WHERE c.name = :name"),
@NamedQuery(name = "Customer.findByEmail", query = "SELECT c FROM Customer c WHERE c.email = :email"),
(...)
i have a session named CutomerFacade
@Stateless
public class CustomerFacade extends AbstractFacade<Customer> {
@PersistenceContext(unitName = "AffableBeanPU")
public EntityManager em;
public List<Customer> GetPorEmail(String match){
return em.createNamedQuery("Customer.findByEmail").setParameter("email", match).getResultList();
}
Whats the solution to avoid null in entity manager
回答1:
Ensure you are running in a Java EE server and accessing the SessionBean through the container.
What server are you running in? How do you access the SessionBean?
来源:https://stackoverflow.com/questions/16006320/entitymanager-null-and-nullpointerexception-while-searching-in-db