반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- @Controller
- 401오류
- java.lang.AssertionError
- .orelseThrow
- abap
- 세션이란
- filter vs interceptor
- Testcode
- for all entries in
- springSecurityFilterChain 오류
- abap value in field Data Class error
- Validation
- BindingResult
- spring
- controller
- 인터셉터의 정의
- MVC
- 쿠키의 정의
- SpringMVC
- 필터의 정의
- spring MVC
- 쿠키란
- SAP
- application-properties
- jpa
- 필터vs인터셉터
- optional
- n+1
- 김영한
- 구글 보안 api 활용
Archives
- Today
- Total
SAP공장
JPA Service 계층 오류 (Optional) 본문
반응형
public interface MemberRepository extends JpaRepository<Member, Long> {
Optional<Member> findByEmail(String email);
다음과 같이 만들고
public Long addCart(CartItemDto cartItemDto, String email){
Item item = itemRepository.findById(cartItemDto.getItemId()).orElseThrow(EntityNotFoundException::new);
Member member = memberRepository.findByEmail(email);
컴파일 오류발생
핵심은 Optional 을 사용했으면 .orElseThrow를 통해 Service를 날려 줘야 했던것!
<추가줘야할 부분>
.orElseThrow(EntityNotFoundException::new)
public Long addCart(CartItemDto cartItemDto, String email){
Item item = itemRepository.findById(cartItemDto.getItemId()).orElseThrow(EntityNotFoundException::new);
Member member = memberRepository.findByEmail(email)
.orElseThrow(EntityNotFoundException::new);
다음처럼 변경!
if문을 활용하여 member 가 nulll 일 경우에는 UsernameNotFoundException을 통해 해결해 주도록 코딩
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
Member member = memberRepository.findByEmail(email).orElseThrow(EntityNotFoundException::new);
if (member == null) {
throw new UsernameNotFoundException(email);
}
반응형
'[WEB]오류들 > inteilij-git오류' 카테고리의 다른 글
JAVA-HOME not found in your enviroment. 오류 (0) | 2022.01.22 |
---|---|
intellij 에서 index.html 파일이 읽히지 않는 현상 (0) | 2022.01.17 |
JavaMailSender 오류 -JavaMailSender' in your configuration. (0) | 2022.01.16 |
Intellij @valid, validation 오류 (0) | 2022.01.16 |
병합 오류: refusing to merge unrelated histories, 푸시가 거부됨 (0) | 2022.01.10 |