# [JPA] JPQL ๋ฌธ๋ฒ• 4 - ๋‹คํ˜•์„ฑ ์ฟผ๋ฆฌ, ์—”ํ‹ฐํ‹ฐ ์ง์ ‘์‚ฌ์šฉ, Named ์ฟผ๋ฆฌ
Study Repository

[JPA] JPQL ๋ฌธ๋ฒ• 4 - ๋‹คํ˜•์„ฑ ์ฟผ๋ฆฌ, ์—”ํ‹ฐํ‹ฐ ์ง์ ‘์‚ฌ์šฉ, Named ์ฟผ๋ฆฌ

by rlaehddnd0422

๋‹คํ˜•์„ฑ ์ฟผ๋ฆฌ

JPQL์œผ๋กœ ๋ถ€๋ชจ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์กฐํšŒํ•˜๋ฉด ์ž๋™์œผ๋กœ ๋ชจ๋“  ์ž์‹ ์—”ํ‹ฐํ‹ฐ๊นŒ์ง€ ํ•จ๊ป˜ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค. 

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "DTYPE")
public abstract class Item {

    @Id @GeneratedValue
    @Column(name = "ITEM_ID")
    private Long id;

    private String name;
    private int price;
    private int stockQuantity;
}

JPQL

SELECT i
FROM Item i

๋‹จ์ผ ํ…Œ์ด๋ธ” ์ „๋žต์„ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ ์‹คํ–‰๋˜๋Š” SQL

select * from Item i

์กฐ์ธ ์ „๋žต์„ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ ์‹คํ–‰๋˜๋Š” SQL

select
    item0_.ITEM_ID as ITEM_ID2_2_,
    item0_.name as name3_2_,
    item0_.price as price4_2_,
    item0_.stockQuantity as stockQua5_2_,
    item0_3_.author as author1_1_,
    item0_3_.isbn as isbn2_1_,
    item0_.DTYPE as DTYPE1_2_ 
from
    Item item0_ 
left outer join
    Album item0_1_ 
        on item0_.ITEM_ID=item0_1_.ITEM_ID 
left outer join
    Movie item0_2_ 
        on item0_.ITEM_ID=item0_2_.ITEM_ID 
left outer join
    Book item0_3_ 
        on item0_.ITEM_ID=item0_3_.ITEM_ID

์‹คํ–‰๋˜๋Š” SQL์™€ ๊ฐ™์ด ๋ชจ๋“  ์ž์‹ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์™ธ๋ถ€ ์กฐ์ธํ•˜์—ฌ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.

 

+ ์กฐํšŒ๋Œ€์ƒ์„ ๋ชจ๋“  ์ž์‹ ์—”ํ‹ฐํ‹ฐ๊ฐ€ ์•„๋‹Œ ํŠน์ • ์ž์‹ ์—”ํ‹ฐํ‹ฐ๋กœ ํ•œ์ •ํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ์š”?

TYPE์€ ์—”ํ‹ฐํ‹ฐ์˜ ์ƒ์†๊ตฌ์กฐ์—์„œ ์กฐํšŒ๋Œ€์ƒ์„ ํŠน์ • ์ž์‹ ํƒ€์ž…์œผ๋กœ๋งŒ ํ•œ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

// JPQL
SELECT i
FROM Item i 
where TYPE(i) IN (Book, Movie)

 

+ JPQL์—์„œ ๋ถ€๋ชจ ํƒ€์ž…์„ ํŠน์ • ์ž์‹ ํƒ€์ž…์œผ๋กœ ๋‹ค๋ฃฐ ๋•Œ, ์ฆ‰ ๋ถ€๋ชจ ํƒ€์ž…์„ ์ž์‹ ํƒ€์ž…์œผ๋กœ ๋‹ค์šด ์บ์ŠคํŒ… ํ•ด์„œ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ์š”?

JPA 2.1๋ถ€ํ„ฐ TREAT ๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ๋ถ€๋ชจํƒ€์ž…์„ ์ž์‹์œผ๋กœ ๋‹ค์šด์บ์ŠคํŒ…ํ•ด์„œ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

// JPQL 
SELECT i
FROM Item i
WHERE treat(i as Book).author = 'kim'

์—”ํ‹ฐํ‹ฐ ์ง์ ‘ ์‚ฌ์šฉ

JPQL์—์„œ ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ• ๊นŒ์š”?

์—ฌ๊ธฐ์„œ ์ง์ ‘์‚ฌ์šฉํ•œ๋‹ค๋Š” ๋ง์€ select m from Member m ์ฒ˜๋Ÿผ ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ selectํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด ์ž์ฒด๋ฅผ selectํ•˜๋Š” ๊ฒƒ์„ ๋งํ•ฉ๋‹ˆ๋‹ค.
  • JPQL : select count(m.id) from Member m  // ์—”ํ‹ฐํ‹ฐ์˜ ID๋ฅผ ์‚ฌ์šฉ
    • ์—”ํ‹ฐํ‹ฐ์˜ ํ”„๋กœํผํ‹ฐ๋กœ ์กฐํšŒ
  • JPQL : select count(m) from Member m // ์—”ํ‹ฐํ‹ฐ ์ž์ฒด๋ฅผ ์ง์ ‘ ์‚ฌ์šฉ
    • ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด๋ฅผ JPQL์—์„œ ์ง์ ‘ ์‚ฌ์šฉํ•˜๋ฉด SQL์—์„œ๋Š” ํ•ด๋‹น ์—”ํ‹ฐํ‹ฐ์˜ ๊ธฐ๋ณธ ํ‚ค ๊ฐ’์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
      • ์‹คํ–‰๋˜๋Š” SQL : select count(m.id) as cnt from Member m
  • JPQL : select m from Member m where m.team =: team
    • ์ด ๊ฒฝ์šฐ์— ๋ฌต์‹œ์  ๋‚ด๋ถ€ ์กฐ์ธ์ด ์ผ์–ด๋‚  ๊ฒƒ ๊ฐ™์ง€๋งŒ, MEMBER ํ…Œ์ด๋ธ”์ด team_id ์™ธ๋ž˜ํ‚ค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฏ€๋กœ ๋ฌต์‹œ์  ์กฐ์ธ์ด ์ผ์–ด๋‚˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    • ์ฆ‰, ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด๋กœ ์กฐํšŒํ•  ๋•Œ, ๊ธฐ๋ณธํ‚ค๊ฐ€ ์•„๋‹Œ ์™ธ๋ž˜ํ‚ค๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์กฐํšŒํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” ์กฐํšŒํ•˜๋Š” ์—”ํ‹ฐํ‹ฐ๊ฐ€ ์™ธ๋ž˜ํ‚ค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์„ ๋•Œ, ๋ฌต์‹œ์  ์กฐ์ธ์ด ์ผ์–ด๋‚˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    • ์™ธ๋ž˜ํ‚ค๊ฐ€ ์•„๋‹Œ ํ”„๋กœํผํ‹ฐ๋กœ ์กฐํšŒํ•˜๋Š” ๊ฒฝ์šฐ(์˜ˆ๋ฅผ๋“ค๋ฉด m.team.name)์—๋Š” ๋ฌต์‹œ์  ์กฐ์ธ์ด ์ผ์–ด๋‚ฉ๋‹ˆ๋‹ค.  

Named ์ •์  ์ฟผ๋ฆฌ

JPQL์€ ํฌ๊ฒŒ ๋™์ ์ฟผ๋ฆฌ์™€ ์ •์ ์ฟผ๋ฆฌ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

  • ๋™์  ์ฟผ๋ฆฌ : em.createQuery("select ... ")์ฒ˜๋Ÿผ JPQL์„ ๋ฌธ์ž๋กœ ์™„์„ฑํ•ด์„œ ์ง์ ‘ ๋„˜๊ธฐ๋Š” ์ฟผ๋ฆฌ ๋ฐฉ์‹
  • ์ •์  ์ฟผ๋ฆฌ : ๋ฏธ๋ฆฌ ์ •์˜ํ•œ ์ฟผ๋ฆฌ์— ์ด๋ฆ„์„ ๋ถ€์—ฌํ•ด์„œ ํ•„์š”ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ Named ์ฟผ๋ฆฌ๋ผ๊ณ  ๋ถ€๋ฆ…๋‹ˆ๋‹ค.

Named ์ฟผ๋ฆฌ ์‚ฌ์šฉ๋ฒ•

 

1.  NamedQuery๋ฅผ ์–ด๋…ธํ…Œ์ด์…˜์— ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

@Entity
@NamedQuery(name = "myquery1", query = "select ...")
public class Member {
	...
}

2. ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ ๊ณณ์— 1์˜ name์„ ๋„˜๊ฒจ์ค๋‹ˆ๋‹ค.

- ์ด ๋•Œ em.createQuery๊ฐ€ ์•„๋‹Œ em.createNamedQuery๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

List<Member> resultList = em.createNamedQuery("myQuery", Member.class).getResultList();
ํ•œ ์—”ํ‹ฐํ‹ฐ์— ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ •์  ์ฟผ๋ฆฌ๋ฅผ ๋งŒ๋“ค๊ณ  ์‹ถ์€ ๊ฒจ์šฐ์—๋Š” 1์—์„œ @NamedQueries๋‚ด์— @NamedQuery๋กœ ๋“ฑ๋กํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

<์ฐธ๊ณ ์ž๋ฃŒ>

 

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

Study Repository

rlaehddnd0422

ํ™œ๋™ํ•˜๊ธฐ