๊ธฐ์กด์๋ ๊ธ ๋ชฉ๋ก์ ์กฐํ ์ JpaRepository์์ ์๋์ ๊ฐ์ ์ฟผ๋ฆฌ๋ฅผ ์คํํ์๋ค.
@Query("""
SELECT p FROM Post p
WHERE p.status = :status
ORDER BY p.createdAt DESC, p.id DESC
""")
Slice<Post> findPublishedPosts(@Param("status") PostStatus status, Pageable pageable);
์ค์ ์คํ๋ ์ฟผ๋ฆฌ๋ ์๋์ ๊ฐ๋ค.
Hibernate:
select
p1_0.id,
p1_0.category_id,
p1_0.content,
-- ๋ฑ๋ฑ ํ๋๋ค
from
post p1_0
where
p1_0.status=?
order by
p1_0.created_at desc,
p1_0.id desc
fetch
first ? rows only
Hibernate:
select
c1_0.id,
c1_0.created_at,
-- ๋ฑ๋ฑ ํ๋๋ค
from
category c1_0
where
c1_0.id=?
Hibernate:
select
c1_0.id,
c1_0.created_at,
-- ๋ฑ๋ฑ ํ๋๋ค
from
category c1_0
where
c1_0.id=?
-- ์ดํ ์นดํ
๊ณ ๋ฆฌ ๊ฐ์๋งํผ ๋ฐ๋ณต
JPA @ManyToOne์ ๊ธฐ๋ณธ fetch ์ ๋ต์ EAGER์ด์ง๋ง Hibernate๊ฐ SELECT๋ก ๊ฐ์ ธ์์ N+1 ๋ฌธ์ ๊ฐ ๋ฐ์ํ ๊ฒ์ผ๋ก ๋ณด์๋ค.
์ฟผ๋ฆฌ์์ ๋ช
์์ ์ผ๋ก ์กฐ์ธ ๋ก๋ฉ์ ์ง์ํ๋ค.
@Query("""
SELECT p FROM Post p
LEFT JOIN FETCH p.category
WHERE p.status = :status
ORDER BY p.createdAt DESC, p.id DESC
""")
Slice<Post> findPublishedPosts(@Param("status") PostStatus status, Pageable pageable);
* category๊ฐ nullable์ด๊ธฐ ๋๋ฌธ์ LEFT JOIN์ ์ ํํ์๋ค.
์ฟผ๋ฆฌ๋ฅผ ์์ ํ์ง ์๊ณ @EntityGraph๋ฅผ ์ฌ์ฉํ๋ค.
@EntityGraph(attributePaths = { "category" })
@Query("""
SELECT p FROM Post p
WHERE p.status = :status
ORDER BY p.createdAt DESC, p.id DESC
""")
Slice<Post> findPublishedPosts(@Param("status") PostStatus status, Pageable pageable);
์ ๋ ๊ฐ์ ๋ฐฉ๋ฒ ๋ชจ๋ ์๋์ ๊ฐ์ ์ฟผ๋ฆฌ๊ฐ ์คํ๋์๋ค.
Hibernate:
select
p1_0.id,
c1_0.id,
c1_0.created_at,
-- ๋ฑ๋ฑ ํ๋๋ค
from
post p1_0
left join
category c1_0
on c1_0.id=p1_0.category_id
where
p1_0.status=?
order by
p1_0.created_at desc,
p1_0.id desc
fetch join์ ๋ช
์์ ์ผ๋ก @Query ์์ ์์ฑํ๋ ๊ฒฝ์ฐ ์กฐ์ธ ํ์
๊ณผ ์กฐ๊ฑด์ ๋ช
ํํ๊ฒ ์ ์ดํ ์ ์๋ค๋ ์ ์ด ์ฅ์ ์ด์ง๋ง ํ์์ ์์๋ ๊ธ ๋ชฉ๋ก ์กฐํ ์ฟผ๋ฆฌ๊ฐ ๋ณต์กํ์ง ์๊ธฐ ๋๋ฌธ์ ๊ฐ๋
์ฑ์ด ์ข์ @EntityGraph๋ฅผ ์ฌ์ฉํ๊ธฐ๋ก ๊ฒฐ์ ํ์๋ค.
