2021 10 19
2021-10-19
BatchSize
fetchJoin 했는데 아무것도 안 가져온다?
@Query(value = "select distinct feed " +
"from Feed as feed " +
"join fetch feed.author " +
"join fetch feed.comments")
List<Feed> findAllWithFetchJoin();
- 해당 쿼리는 Comment가 최소 한 개가 있는 피드들만 가져옴
- 해당 쿼리 보면, inner join 문을 통해서 comment와 feed를 join함 (on feed.id = comment.feed_id)
- inner join이 뭐야. comment, feed있어야 (교집합) 테이블 조인해서 값 가져오는 거자나
- 그러다 보니 Comment가 한개도 없는 피드들은 해당 쿼리로는 못가져옴.
- 이러면 커멘트가 한개도 없는 feed들도 추출하고 싶다면 left join을 활용하자
- feed를 중심으로 집합을 때려버리는 걸로 해야함
- left join 하면서 댓글 없는 피드들의 comment 관련 필드들은 null로 채워지면서 엔티티 조회가 될 것임
@Query(value = "select distinct feed " +
"from Feed as feed " +
"join fetch feed.author " +
"left join fetch feed.comments")
List<Feed> findAllWithFetchJoin();