@Test
public void returnType() {
Member m1 = new Member("AAA", 10);
Member m2 = new Member("AAA", 20);
memberRepository.save(m1);
memberRepository.save(m2);
Optional<Member> findMember = memberRepository.findOptionalByUsername("AAA");
System.out.println("findMember = " + findMember);
}
✔ JPA에서는 NonUniqueResultException이 나오지만
Spring은 JPA만 repository기술로 사용하는 것이 아니기때문에 IncorrectResultSizeDataAccessException로 변환하여 반환해준다.
실제로 테스트를 해보았는데 IncorrectResultSizeDataAccessException로 예외가 나오는 것을 알 수 있었다.
@Test
public void returnType() {
Member m1 = new Member("AAA", 10);
Member m2 = new Member("AAA", 20);
memberRepository.save(m1);
memberRepository.save(m2);
assertThatThrownBy(() -> memberRepository.findOptionalByUsername("AAA")).isInstanceOf(IncorrectResultSizeDataAccessException.class);
}
✔ NonUniqueResultException로 예외처리를 하면 오류가 난다.
@Test
public void returnType() {
Member m1 = new Member("AAA", 10);
Member m2 = new Member("AAA", 20);
memberRepository.save(m1);
memberRepository.save(m2);
assertThatThrownBy(() -> memberRepository.findOptionalByUsername("AAA")).isInstanceOf(NonUniqueResultException.class);
}