반응형
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
- 필터vs인터셉터
- abap value in field Data Class error
- 세션의 정의
- Validation
- n+1
- 쿠키란
- MVC
- spring MVC
- SpringMVC
- .orelseThrow
- 유연한 컨트롤러1 - v5
- @Controller
- 구글 보안 api 활용
- filter vs interceptor
- 세션vs쿠키
- Testcode
- jpa
- 401오류
- controller
- 김영한
- optional
- java.lang.AssertionError
- BindingResult
- 인터셉터의 정의
- springSecurityFilterChain 오류
- application-properties
- spring
- 세션이란
- 필터의 정의
- 쿠키의 정의
Archives
- Today
- Total
ABAP DUMP ERROR 24시
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 |
Comments