I have written code for logging the user in from the home page but I am encountering javax.ejb.EJBException. My web application makes use of JPA, EJB and JSF.
In my entity class I have included the following Named Query.
@NamedQuery(name = "Users.findByUsernameAndPassword", query = "SELECT u FROM Users u WHERE u.username = :username AND u.password = :password"),
In my stateless session bean I have created the following business method:
public Users AuthenticateUser(String username, String password) throws EJBException, SQLException {
Query q = em.createNamedQuery("Users.findByUsernameAndPassword");
q.setParameter("username", username);
q.setParameter("password", password);
return (Users)q.getSingleResult();
}
In my EJB client I am calling this method as follows:
user = usersFacade.AuthenticateUser(username, password);
if(user != null) {
// Reditect to home page.
} else {
// Show an error message.
}
I am getting the exception on entering an incorrect password. On entering the correct password things are working fine. On entering incorrect password I am unable to redirect the user to the home page and throw an error message as was intended in the code of the managed bean.
Please help me understand what is going wrong as I am new to Java EE 7. Am I not able to handle EJBException and/or SQLException properly? Please help.