2021. 10. 5. 22:28γBack-End μμ μ€/Spring Framework
πJPQLκ³Ό left ( outer ) join
λͺ©λ‘ νλ©΄μμ κ²μκΈμ μ 보μ ν¨κ» λκΈμ μλ₯Ό κ°μ΄ κ°μ Έμ€κΈ° μν΄μλ λ¨μν νλμ μν°ν° νμ μ μ΄μ©ν μ μλ κ²μ΄μμ. μ΄μ λν ν΄κ²°μ± μ€ νλκ° λ§μ΄ μ¬μ©λλ JPQLμ Join(μ‘°μΈ)μ μ΄μ©ν΄μ μ²λ¦¬νλ κ²μ΄μμ.
"μ΄ ν¬μ€ν μ μΏ ν‘ ννΈλμ€ νλμ μΌνμΌλ‘, μ΄μ λ°λ₯Έ μΌμ μ‘μ μμλ£λ₯Ό μ 곡λ°μ΅λλ€."
π left ( outer ) join
Spring Boot 2 Version μ΄ν ν¬ν¨λλ JPA λ²μ μ μν°ν° ν΄λμ€ λ΄μ μ ν μκ΄ κ΄κ³κ° μλλΌλ μ‘°μΈμ μ΄μ©ν μ μλ κ²μ΄μμ. μ‘°μΈμ ν λ 'INNER JOIN νΉμ JOIN' κ³Ό κ°μ΄ μΌλ°μ μΈ μ‘°μΈμ μ΄μ©ν μλ μκ³ , 'LEFT OUTER JOIN νΉμ LEFT JOIN'μ μ΄μ©ν μλ μλ κ²μ΄μμ.
μ£Όλνλμ SystemUseManul μν°ν° ν΄λμ€μ λ΄λΆμλ Member μν°ν° ν΄λμ€λ₯Ό λ³μλ‘ μ μΈνκ³ , μ°κ΄ κ΄κ³λ₯Ό λ§Ίκ³ μλ κ²μ΄μμ. μ΄λ° κ²½μ° SystemUseManul μ writer λ³μλ₯Ό μ΄μ©ν΄μ μ‘°μΈμ μ²λ¦¬νλ κ²μ΄μμ.
getManualWithWriter() λ SystemUseManual λ₯Ό μ¬μ©νκ³ μμΌλ, Member λ₯Ό κ°μ΄ μ‘°νν΄μΌ νλ μν©μΈ κ²μ΄μμ. SystemUseManual ν΄λμ€μλ Memberμμ μ°κ΄ κ΄κ³λ₯Ό λ§Ίκ³ μκΈ° λλ¬Έμ m.writerμ κ°μ ννλ‘ μ¬μ©ν΄μΌ νλ κ²μ΄μμ. μ΄μ²λΌ λ΄λΆμ μν°ν°λ₯Ό μ΄μ©ν λλ 'LEFT JOIN' λ€μ 'ON'μ μ΄μ©νλ λΆλΆμ΄ μλ κ²μ΄μμ. μμ±ν getManualWithWriter()λ₯Ό ν μ€νΈ μ½λλ‘ νμΈ ν΄ λ³Όκ²μ!
κ²°κ³Όλ₯Ό 보면 Lazy Loading (μ§μ° λ‘λ©)μ μ¬μ©νμ¬ μ²λ¦¬νμΌλ, μ€νλλ 쿼리λ₯Ό 보면 μ‘°μΈ μ²λ¦¬κ° λμ΄ ν λ²μ SystemUseManual ν μ΄λΈκ³Ό member ν μ΄λΈμ μ΄μ©νλ κ²μ λ³Ό μ μλ κ²μ΄μμ!
π μ°κ΄ κ΄κ³κ° μλ Entity Join μ²λ¦¬μλ on
SystemUseManual κ³Ό Member μ¬μ΄μλ λ΄λΆμ μΌλ‘ μ°Έμ‘°λ₯Ό ν΅ν΄ μ°κ΄ κ΄κ³κ° μμ§λ§, SystemUseManual μ Comment λ μν©μ΄ λ€λ₯Έ κ²μ΄μμ. Comment μͺ½μ΄ @ManyToOneμΌλ‘ μ°Έμ‘°νκ³ μμ§λ§, SystemUseManual μ μ₯μμλ Comment κ°μ²΄λ€μ μ°Έμ‘°νκ³ μμ§ μκΈ° λλ¬Έμ λ¬Έμ κ° λλ κ²μ΄μμ. μ΄λ° κ²½μ° μ§μ μ‘°μΈμ νμν 쑰건μ 'on'μ μ΄μ©ν΄μ μμ±ν΄ μ£Όμ΄μΌ νλ΅λλ€!
'νΉμ κ²μλ¬Όκ³Ό ν΄λΉ κ²μλ¬Όμ μν λκΈμμ μ‘°ν' ν΄μΌ νλ μν©μ μκ°νλ©΄ SystemUseManual κ³Ό Comment ν μ΄λΈμ μ‘°μΈν΄μ 쿼리λ₯Ό μμ±νκ² λ©λλ€. μλ₯Ό λ€μ΄ νμ¬ DB λ΄μ 100λ² κ²μκΈμ λ κ°μ λ κΈμ κ°μ§κ³ μλ€κ³ κ°μ ν λ, μ΄λ₯Ό μμν SQLλ‘ μ²λ¦¬νλ€λ©΄ λ€μκ³Ό κ°μ΄ μμ±ν μ μλ κ²μ΄μμ.
select systemUseManual.no, systemUseManual.title, systemUseManual.writer_email, manualCommentNo, commentContent
from systemUseManual left outer join SystemUseManualCommnet comment
on comment.systemUseManual .no = systemUseManual .no
where systemUseManual.no = 100;
μμ 쿼리λ₯Ό JPQLλ‘ μ²λ¦¬νλ©΄ μλμ κ°μ κ²μ΄μμ!
μ°κ° κ΄κ³κ° μλ κ²½μ°μ λΉκ΅ν΄ 보면 μ€κ°μ 'on'μ΄ μ¬μ©λλ©΄μ μ‘°μΈ μ‘°κ±΄μ μ§μ μ§μ νλ λΆλΆμ΄ μΆκ°λ κ²μ λ³Ό μ μλ κ²μ΄μμ. ν μ€νΈ μ½λλ₯Ό νλ² μμ±ν΄ λ³Όκ²μ!
μμ λ΄μ©μ μ 리ν΄λ³΄μλ©΄ μλμ κ°μ κ²μ΄μμ.
- κ²μλ¬Ό(SystemUseManual) : κ²μλ¬Όμ λ²νΈ, μ λͺ©, κ²μλ¬Όμ μμ± μκ°
- νμ(Member) : νμμ μ΄λ¦ / μ΄λ©μΌ
- λκΈ(Comment) : ν΄λΉ κ²μλ¬Όμ λκΈ μ
μμ μΈ κ°μ μν°ν° μ€μ κ°μ₯ λ§μ λ°μ΄ν°λ₯Ό κ°μ Έμ€λ μͺ½μ SystemUseManual μ΄λ―λ‘, μ΄ μΉκ΅¬λ₯Ό μ€μ¬μΌλ‘ μ‘°μΈ κ΄κ³λ₯Ό μμ±νλ κ²μ΄μμ. Memberλ SystemUseManual λ΄μ writerλΌλ νλλ‘ μ°κ΄ κ΄κ³λ₯Ό λ§Ίκ³ μκ³ , Comment λ μ°κ΄ κ΄κ³κ° μλ μν©μΈ κ²μ΄μμ. μ‘°μΈ νμλ SystemUseManual λ₯Ό κΈ°μ€μΌλ‘ 'GROUP BY' μ²λ¦¬ νμ¬ νλμ κ²μλ¬Ό λΉ νλμ λΌμΈμ΄ λ μ μλλ‘ μ²λ¦¬(JPQLμ μ΄μ©νλ©΄ SQLμ μ¬μ©νλ λ§μ ν¨μ μ μ© κ°λ₯)ν΄μΌ νλ κ²μ΄μμ.
SystemUseManualRepositoryμλ Pageableμ λ§€κ° λ³μλ‘ μ λ¬λ°κ³ , Page<Object[]> λ°ν νμ μ getManualWithCommentCount()λ₯Ό μλμ κ°μ΄ μμ±ν΄ λ³Ό κ²μ΄μμ.
Hibernate:
select
systemusem0_.no as col_0_0_,
member1_.id as col_1_0_,
count(systemusem2_.manual_comment_no) as col_2_0_,
systemusem0_.no as no1_1_0_,
member1_.id as id1_0_1_,
systemusem0_.moddate as moddate2_1_0_,
systemusem0_.regdate as regdate3_1_0_,
systemusem0_.content as content4_1_0_,
systemusem0_.title as title5_1_0_,
systemusem0_.writer_id as writer_i6_1_0_,
member1_.moddate as moddate2_0_1_,
member1_.regdate as regdate3_0_1_,
member1_.email as email4_0_1_,
member1_.grade as grade5_0_1_,
member1_.name as name6_0_1_,
member1_.password as password7_0_1_,
member1_.phone as phone8_0_1_
from
system_use_manual systemusem0_
left outer join
member member1_
on systemusem0_.writer_id=member1_.id
left outer join
system_use_manual_comment systemusem2_
on (
systemusem2_.manual_no=systemusem0_.no
)
group by
systemusem0_.no
order by
systemusem0_.no desc limit ?
Hibernate:
select
count(systemusem0_.no) as col_0_0_
from
system_use_manual systemusem0_
[com.hongga.junyharang.domain.entity.manual.SystemUseManual@570ede3b, com.hongga.junyharang.domain.entity.member.Member@135f160e, 0]
[com.hongga.junyharang.domain.entity.manual.SystemUseManual@299cddbf, com.hongga.junyharang.domain.entity.member.Member@31beb0d0, 1]
1νμ΄μ§μ λ°μ΄ν°λ₯Ό μ²λ¦¬νλ€κ³ κ°μ νκ³ , νμ΄μ§ λ²νΈλ 0μΌλ‘ μ§μ νκ³ , 2κ°λ₯Ό μ‘°νν κ²μ΄μμ.
"μ΄ ν¬μ€ν μ μΏ ν‘ ννΈλμ€ νλμ μΌνμΌλ‘, μ΄μ λ°λ₯Έ μΌμ μ‘μ μμλ£λ₯Ό μ 곡λ°μ΅λλ€."
'Back-End μμ μ€ > Spring Framework' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Spring] μμ‘΄κ΄κ³ μ£Όμ λ°©λ² (0) | 2021.10.07 |
---|---|
[Spring] Component Scan And Auto wired Start (0) | 2021.10.06 |
[JPA] @ManyToOne (0) | 2021.10.05 |
[Spring] μ±κΈν€ 컨ν μ΄λ (0) | 2021.10.05 |
[Spring] μ€νλ§ μ»¨ν μ΄λ μμ± (0) | 2021.10.03 |