BankSystem, 2021년 12월28일

yshjft·2021년 12월 28일
0

Bank System

목록 보기
2/14

EmptyResultDataAccessException

Data access exception thrown when a result was expected to have at least one row (or element) but zero rows (or elements) were actually returned.

위에 나온대로 데이터가 최소 1개는 나와야 하는데 1개도 나오지 않은 경우 발생하는 예외다.

 public User findUserByEmail(String email) {
        return em.createQuery("select user from User user where user.email = :email", User.class)
                .setParameter("email", email)
                .getSingleResult();
    }

원래 계획은 return을 Optional로 처리하는 것이었으나 em.createQuery로 조회를 할 경우 EmptyResultDataAccessException이 발생하여 할 수 없었다.

try {
            // 이메일 대조
            User user = userRepository.findUserByEmail(loginRequestDto.getEmail());

	    ...
        }catch (EmptyResultDataAccessException e) {
            throw new LoginException("LOGIN_FAIL_EMAIL");
        }

따라서 위와 같이 try catch로 예외를 처리하였다.

참고

profile
꾸준히 나아가자 🐢

0개의 댓글