开发者

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

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){
        throwExc开发者_StackOverflow中文版eption(username, e);
        return null;
    }
}

Does anybody know what the problem is. :(

I would like to return null and don`t get an Exception.

Thank you very much


You seem to rethrow the exception in your catch block with the statement throwException(username, e);. If you expect to get the user or null without any exception this should look like the following:

public User getUserByUsernameOrNull(String username) {
    try{
        Query q = em.createNamedQuery(User.getUserByUsername);
        q.setParameter("username", username);
        return (User) q.getSingleResult();
    } catch(NoResultException e) {
        return null;
    }
}


Use getResultList instead and check if the List is empty (has zero element). Otherwise, the list contains one element and you simply return it.


You experience the defined behaviour when calling getSingleResult and none entry was found: A NoResultException is thrown. You can catch NoResultException in the catch-clause, because the transaction won't be marked as rollback, when JPA is throwing NoResultException. Or you can use getResultList() and check if size is exactly "1", so you know you have found your user.

Additionally, I wouldn't return "[null]" if the user is not found, but throw a checked UserNotFoundException (to be defined). But this depends on the contract of the method you are going to implement.


Michael said: "You can catch NoResultException in the catch-clause, because the transaction won't be marked as rollback, when JPA is throwing NoResultException.". It looks like for some jpa implementations, the NoResultException will rollbak transaction, which violates jpa specification, according to this article: NoResultException marks transaction rollback


If your application uses Spring Roo the most voted answer doesn't work: the exception is caught by the aspects and you never get the desired NoResultException. The only way to solve the issue is to use getResultList and to check for zero, one or more results in the resulting List, as stated in the second most voted answer.


try {
            Query queryObj = entityMgrObj
                    .createQuery("SELECT u FROM UserEntity u WHERE u.email = :email AND u.password = :password");
            queryObj.setParameter("email", email);
            queryObj.setParameter("password", password);
            UserEntity userEntity = (UserEntity) queryObj.getSingleResult();

            return userEntity;

        } catch (NoResultException e) {
            return null;
        }


What does the method throwException do?

Is an exception being thrown within it but you are using the prior exception's message?


Catch NoResultException in try-catch block and handle it as per your requirement such as returning a null value.

Example of try-catch is detailed in the following link: http://www.javabrahman.com/j2ee/jpa/no-result-exception/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜