1

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.

2
  • I would further like to add that 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. Commented Sep 14, 2015 at 17:25
  • Remarkable upvote btw Commented Sep 14, 2015 at 18:39

1 Answer 1

1

You have not shared the exception which you are getting but getSingleResult throws NoResultException if there is no result so you should wrap your call with a try/catch block and handle this accordingly.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Petros. I did exactly as you had suggested. Now it is really working. Thank you so much.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.