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

后端 未结 8 2556
执笔经年
执笔经年 2020-12-14 09:33

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.         


        
相关标签:
8条回答
  • 2020-12-14 09:53
    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;
            }
    
    0 讨论(0)
  • 2020-12-14 10:12

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

    0 讨论(0)
提交回复
热议问题