๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ–ฅ๏ธ ๋ฐฑ์—”๋“œ/SpringBoot

[JPA] ๊ฐ์ฒด์ง€ํ–ฅ ์ฟผ๋ฆฌ ์–ธ์–ด - ๊ธฐ๋ณธ๋ฌธ๋ฒ• (2)

by OR15A 2023. 11. 26.

์กฐ์ธ

  • ๋‚ด๋ถ€ ์กฐ์ธ
    • SELECT m FROM Member m [INNER] JOIN m.team t
  • ์™ธ๋ถ€ ์กฐ์ธ
    • SELECT m FROM Member m LEFT [OUTER] JOIN m.team t
  • ์„ธํƒ€ ์กฐ์ธ 
    • SELECT count(m) FROM Member m, Team t WHERE m.username = t.name

 

 

ON ์ ˆ

  • ON์ ˆ์„ ํ™œ์šฉํ•œ ์กฐ์ธ(JPA 2.1๋ถ€ํ„ฐ ์ง€์›)
  • 1. ์กฐ์ธ ๋Œ€์ƒ ํ•„ํ„ฐ๋ง
  • 2. ์—ฐ๊ด€๊ด€๊ณ„ ์—†๋Š” ์—”ํ‹ฐํ‹ฐ ์™ธ๋ถ€ ์กฐ์ธ(ํ•˜์ด๋ฒ„๋„ค์ดํŠธ 5.1๋ถ€ํ„ฐ)

 

์กฐ์ธ ๋Œ€์ƒ ํ•„ํ„ฐ๋ง

  • ์˜ˆ) ํšŒ์›๊ณผ ํŒ€์„ ์กฐ์ธํ•˜๋ฉด์„œ, ํŒ€ ์ด๋ฆ„์ด A์ธ ํŒ€๋งŒ ์กฐ์ธ
[JPQL]
SELECT m, t 
FROM Member m LEFT JOIN m.team t on t.name = 'A' 

[SQL]
SELECT m.*, t.* 
FROM Member m LEFT JOIN Team t ON m.TEAM_ID=t.id and t.name='A'

 

์—ฐ๊ด€๊ด€๊ณ„ ์—†๋Š” ์—”ํ‹ฐํ‹ฐ ์™ธ๋ถ€ ์กฐ์ธ

  • ์˜ˆ) ํšŒ์›์˜ ์ด๋ฆ„๊ณผ ํŒ€์˜ ์ด๋ฆ„์ด ๊ฐ™์€ ๋Œ€์ƒ ์™ธ๋ถ€ ์กฐ์ธ
[JPQL]
SELECT m, t 
FROM Member m LEFT JOIN Team t on m.username = t.name

[SQL]
SELECT m.*, t.* 
FROM Member m LEFT JOIN Team t ON m.username = t.name

 

 

์„œ๋ธŒ ์ฟผ๋ฆฌ

  • ๋‚˜์ด๊ฐ€ ํ‰๊ท ๋ณด๋‹ค ๋งŽ์€ ํšŒ์›
    • select m from Member m
      where m.age > (select avg(m2.age) from Member m2) 
  • ํ•œ ๊ฑด์ด๋ผ๋„ ์ฃผ๋ฌธํ•œ ๊ณ ๊ฐ
    • select m from Member m
      where (select count(o) from Order o where m = o.member) > 0 

 

์„œ๋ธŒ ์ฟผ๋ฆฌ ์ง€์› ํ•จ์ˆ˜

  • [NOT] EXISTS (subquery): ์„œ๋ธŒ์ฟผ๋ฆฌ์— ๊ฒฐ๊ณผ๊ฐ€ ์กด์žฌํ•˜๋ฉด ์ฐธ
    • {ALL | ANY | SOME} (subquery)
    •  ALL ๋ชจ๋‘ ๋งŒ์กฑํ•˜๋ฉด ์ฐธ
    • ANY, SOME: ๊ฐ™์€ ์˜๋ฏธ, ์กฐ๊ฑด์„ ํ•˜๋‚˜๋ผ๋„ ๋งŒ์กฑํ•˜๋ฉด ์ฐธ
  • [NOT] IN (subquery): ์„œ๋ธŒ์ฟผ๋ฆฌ์˜ ๊ฒฐ๊ณผ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ๊ฐ™์€ ๊ฒƒ์ด ์žˆ์œผ๋ฉด ์ฐธ
  • ์˜ˆ์ œ
#ํŒ€A ์†Œ์†์ธ ํšŒ์›
select m from Member m
where exists (select t from m.team t where t.name = 'ํŒ€A');


#์ „์ฒด ์ƒํ’ˆ ๊ฐ๊ฐ์˜ ์žฌ๊ณ ๋ณด๋‹ค ์ฃผ๋ฌธ๋Ÿ‰์ด ๋งŽ์€ ์ฃผ๋ฌธ๋“ค
select o from Order o 
where o.orderAmount > ALL (select p.stockAmount from Product p) ;

#์–ด๋–ค ํŒ€์ด๋“  ํŒ€์— ์†Œ์†๋œ ํšŒ์›
select m from Member m 
where m.team = ANY (select t from Team t) ;

 

 

 

JPA ์„œ๋ธŒ ์ฟผ๋ฆฌ ํ•œ๊ณ„

  • JPA๋Š” WHERE, HAVING ์ ˆ์—์„œ๋งŒ ์„œ๋ธŒ ์ฟผ๋ฆฌ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • SELECT ์ ˆ๋„ ๊ฐ€๋Šฅ(ํ•˜์ด๋ฒ„๋„ค์ดํŠธ์—์„œ ์ง€์›)
  • FROM ์ ˆ์˜ ์„œ๋ธŒ ์ฟผ๋ฆฌ๋Š” JPQL์—์„œ ๋ถˆ๊ฐ€๋Šฅ(ํ•˜์ด๋ฒ„๋„ค์ดํŠธ5๊นŒ์ง€)
    • ์กฐ์ธ์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ์œผ๋ฉด ํ’€์–ด์„œ ํ•ด๊ฒฐ
  • ํ•˜์ด๋ฒ„๋„ค์ดํŠธ6๋ถ€ํ„ฐ๋Š” FROM์ ˆ์˜ ์„œ๋ธŒ์ฟผ๋ฆฌ ์ง€์›

 

 

JPQL ํƒ€์ž… ํ‘œํ˜„

  • ๋ฌธ์ž
    • ‘HELLO’, ‘She’’s’
  • ์ˆซ์ž
    • 10L(Long), 10D(Double), 10F(Float)
  • Boolean
    • TRUE, FALSE
  • ENUM
    • jpabook.MemberType.Admin (ํŒจํ‚ค์ง€๋ช… ํฌํ•จ)
  • ์—”ํ‹ฐํ‹ฐ ํƒ€์ž…
    • TYPE(m) = Member (์ƒ์† ๊ด€๊ณ„์—์„œ ์‚ฌ์šฉ)

 

SQL๊ณผ ๋ฌธ๋ฒ•์ด ๊ฐ™์€ ์‹

  • EXISTS, IN
  • AND, OR, NOT
  • =, >, >=, <, <=, <>
  • BETWEEN, LIKE, IS NULL

 

 

์กฐ๊ฑด์‹ (CASE ์‹)

  • ์ฟผ๋ฆฌDSL์“ฐ๋ฉด ๋‹ค ์ง€์›ํ•˜์ง€ ๊ฑฑ์ • ๋ง๊ธฐ
  • ์กฐ๊ฑด์‹ CASE ์‹
select
   case when m.age <= 10 then 'ํ•™์ƒ์š”๊ธˆ'
	    when m.age >= 60 then '๊ฒฝ๋กœ์š”๊ธˆ'
 	    else '์ผ๋ฐ˜์š”๊ธˆ'
   end
from Member m
  • ๋‹จ์ˆœ CASE ์‹
select
   case t.name 
      when 'ํŒ€A' then '์ธ์„ผํ‹ฐ๋ธŒ110%'
      when 'ํŒ€B' then '์ธ์„ผํ‹ฐ๋ธŒ120%'
      else '์ธ์„ผํ‹ฐ๋ธŒ105%'
   end
from Team t
  • COALESCE: ํ•˜๋‚˜์”ฉ ์กฐํšŒํ•ด์„œ null์ด ์•„๋‹ˆ๋ฉด ๋ฐ˜ํ™˜
์‚ฌ์šฉ์ž์ด๋ฆ„์ด ์—†์œผ๋ฉด ์ด๋ฆ„์—†๋Š”ํšŒ์› ๋ฐ˜ํ™˜
select coalesce(m.username,'์ด๋ฆ„ ์—†๋Š” ํšŒ์›') from Member m
  • NULLIF: ๋‘ ๊ฐ’์ด ๊ฐ™์œผ๋ฉด null ๋ฐ˜ํ™˜, ๋‹ค๋ฅด๋ฉด ์ฒซ๋ฒˆ์งธ ๊ฐ’ ๋ฐ˜ํ™˜
์‚ฌ์šฉ์ž ์ด๋ฆ„์ด '๊ด€๋ฆฌ์ž'๋ฉด null์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๋‚˜๋จธ์ง€๋Š” ๋ณธ์ธ์˜ ์ด๋ฆ„์„ ๋ฐ˜ํ™˜
select NULLIF(m.username, '๊ด€๋ฆฌ์ž') from Member m

 

 

JPQL ๊ธฐ๋ณธ ํ•จ์ˆ˜

  • JPQLํ‘œ์ค€ ํ•จ์ˆ˜๋กœ DB ์ƒ๊ด€์—†์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ
  • CONCAT
  • SUBSTRING
  • TRIM
  • LOWER, UPPER
  • LENGTH
  • LOCATE
  • ABS, SQRT, MOD
  • SIZE, INDEX(JPA ์šฉ๋„)
  • ๋‹คํ–‰ํžˆ ์ด ์™ธ์˜  ํ•จ์ˆ˜๋„ ๋ฏธ๋ฆฌ ์ •์˜๋˜์–ด ์žˆ์Œ(DB ์ข…์†์  ํ•จ์ˆ˜)

 

 

 

์‚ฌ์šฉ์ž ์ •์˜ ํ•จ์ˆ˜ ํ˜ธ์ถœ

  • ๊ทธ๋ƒฅ ์‚ฌ์šฉํ•  ์ˆ˜๋Š” ์—†๊ณ , ํ•˜์ด๋ฒ„๋„ค์ดํŠธ๋Š” ์‚ฌ์šฉ์ „ ๋ฐฉ์–ธ์— ์ถ”๊ฐ€ํ•˜๊ธฐ
  • ์‚ฌ์šฉํ•˜๋Š” DB ๋ฐฉ์–ธ์„ ์ƒ์†๋ฐ›๊ณ , ์‚ฌ์šฉ์ž ์ •์˜ ํ•จ์ˆ˜๋ฅผ ๋“ฑ๋กํ•œ๋‹ค
select function('group_concat', i.name) from Item i