예외 처리를 할때 @ExceptionHandler 로 처리를 해줬다.
RestController 단에 달아줬다.
@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<String> handleIllegalArgumentException(IllegalArgumentException exception) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(exception.getMessage());
}
@ExceptionHandler(UsernameNotFoundException.class)
public ResponseEntity<String> handleException(UsernameNotFoundException e) {
return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED).body("아이디가 없습니다.");
}
오늘의 문제의 코드
public MyPageResponseDto allInfo(UserDetailsImpl userDetails) {
// User user = userDetails.getUser();
Long userId = userDetails.getUser().getId();
User user = userRepository.findById(userId).orElse(null);
String nickname = user.getNickname();
List<Post> posts = user.getPosts();
int myRank = checkMyRank(user);
int totalUser = checkTotalUser();
return new MyPageResponseDto(nickname, posts, myRank, totalUser);
}
이전에는 주석친 부분으로 user를 잡아주고, 일대다 관계 맺어진 부분에 즉시로딩으로 연결했었다.
fetch = FetchType.EAGER
이렇게 짜면 예상치 못한 SQL이 발생해서 비효율적이 된다.
그래서 아이디를 받아서 db에서 호출해 와서 받아내는 걸로 바꿨는데
현재도 좋은 방법은 아니라고 피드백을 받았다.
설계 단계부터 이런 상황이 안 생기게 설계를 해야할 것 같다.
즉시로딩,지연로딩
출처 : https://logical-code.tistory.com/140
[JPA] 즉시 로딩/지연 로딩
앞선 포스팅에서 프록시에 대해 알아보았습니다. 이번 포스팅에서는 JPA에서 프록시를 어떻게 활용하는지, 즉시 로딩과 지연 로딩을 통해 알아봅니다. 지연 로딩 (Lazy Loading) 1 2 3 4 5 6 7 8 9 @Entity p
logical-code.tistory.com
JPA 조건절(where) Method
// 단순 조건
public School findByName(String name); // where name = ?
public List<School> findByRegion(String region); // where region = ?
// not
public List<School> findByRegionNot(String region); // where region <> ?
// and
public List<School> findByNameAndRanking(String name, int ranking); // where name = ? and ranking = ?
// or
public List<School> findByNameOrRanking(String name, int ranking); // where name = ? or ranking = ?
// between
public List<School> findByRankingBetween(int startRanking, int endRanking); // where ranking between ? and ?
// 부등호
public List<School> findByRankingLessThan(int ranking); // where ranking < ?
public List<School> findByRankingLessThanEqual(int ranking); // where ranking <= ?
public List<School> findByRankingGreaterThan(int ranking); // where ranking > ?
// null
public List<School> findByRankingIsNull(); // where ranking is null
public List<School> findByRankingIsNotNull(); // where ranking is not null
// like
public List<School> findByNameStartingWith(String name); // where name like ?%
public List<School> findByNameEndingWith(String name); // where name like %?
public List<School> findByNameContaining(String name); // where name like %?%
// in
public List<School> findByIdIn(List<Integer> id); // where school_id in (?, ?, ? ...)
public List<School> findByIdNotIn(List<Integer> id); // where school_id not in (?, ?, ? ...)
// order by
public List<School> findAllByOrderById(); // order by school_id, "AllBy" 주의
public List<School> findByNameContainingOrderByRanking(String name); // where name like %?% order by ranking
public List<School> findByIdOrderByIdDesc(int id); // where id = ? order by id desc
'TIL' 카테고리의 다른 글
항해99_TIL220612 (미니프로젝트 6일차) (0) | 2022.06.15 |
---|---|
항해99_TIL220612 (미니프로젝트 5일차) (0) | 2022.06.14 |
항해99_WIL220612 (CORS) (0) | 2022.06.12 |
항해99_TIL220611 (미니프로젝트 2일차) (0) | 2022.06.11 |
항해99_TIL220610 (미니프로젝트 시작) (0) | 2022.06.10 |