javax.persistence.NoResultException: getSingleResult() did not retrieve any entities

Posted by apple1988 on Stack Overflow See other posts from Stack Overflow or by apple1988
Published on 2010-04-10T10:55:38Z Indexed on 2010/04/10 11:03 UTC
Read the original article Hit count: 258

Filed under:
|
|

Hello,

i have created a namedquery with ejb to check if the username is used. When the singleResult is null, then i get the following Exception :

javax.persistence.NoResultException: getSingleResult() did not retrieve any entities

But this exception is the result that i want when the username is free. ^^

Here is the code:

public User getUserByUsername(String username) throws DAOException{

    try{
        Query q = em.createNamedQuery(User.getUserByUsername);
        q.setParameter("username", username);
        return (User) q.getSingleResult();
    }catch(Exception e){
        throwException(username, e);
        return null;
    }
}

Does anybody know what the problem is. :( I will return null andy don`t get an Exception.

Thank you very much

© Stack Overflow or respective owner

Related posts about java

Related posts about ejb