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

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

by OR15A 2023. 11. 26.

JPQL

  • Java Persistence Query Language
  • ๊ฐ์ฒด์ง€ํ–ฅ ์ฟผ๋ฆฌ ์–ธ์–ด. ํ…Œ์ด๋ธ” ๋Œ€์ƒX  ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด๋ฅผ ๋Œ€์ƒO ์œผ๋กœ ์ฟผ๋ฆฌ
  • SQL์„ ์ถ”์„ฑํ™”ํ•˜๋ฏ€๋กœ ํŠน์ • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค SQL์— ์˜์กดํ•˜์ง€ ์•Š์Œ
  • ๊ฒฐ๊ตญ SQL๋กœ ๋ณ€ํ™˜

 

JPQL ๋ฌธ๋ฒ•

  • select m from Member as m where m.age > 14
  • ์—”ํ‹ฐํ‹ฐ์™€ ์†์„ฑ์€ ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ํ•จ O(Memeber, age ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง„ ๊ฒƒ๊ณผ ๋Œ€์†Œ๋ฌธ์ž ๋™์ผํ•ด์•ผ)
  • JPQL ํ‚ค์›Œ๋“œ๋Š” ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์Œ(SELECT select FROM from WHERE where)
  • ์—”ํ‹ฐํ‹ฐ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•œ๋‹ค(ํ…Œ์ด๋ธ” ์ด๋ฆ„์ด ์•„๋‹˜)
  • ๋ณ„์นญ์ด ํ•„์ˆ˜ m (as๋Š” ์ƒ๋žต ๊ฐ€๋Šฅํ•˜์ง€๋งŒ ๊ฐ€๊ธ‰์  ์“ฐ๋ฉด ์ข‹์Œ)
  • ์ง‘ํ•ฉ, ์ •๋ ฌ ๋“ฑ ํ•จ์ˆ˜ ๊ฐ€๋Šฅ (COUNT, SUM, AVG, MAX, GROUP BY, ORDER BY ๋“ฑ)

 

 

TypeQuery

  • ๋ฐ˜ํ™˜ํƒ€์ž…์ด ๋ช…ํ™•ํ•  ๋•Œ
TypeQuery<Member> query1 = em.createQuery("select m from Member m", Member.class);
TypeQuery<String> query2 = em.createQuery("select m.username from Member m", String.class);

Query

  • ๋ฐ˜ํ™˜ํƒ€์ž…์ด ๋ช…ํ™•ํ•˜์ง€ ์•Š์„ ๋•Œ
Query query3 = em.createQuery("select m.username, m.age from Member m");

 

 

๊ฒฐ๊ณผ ์กฐํšŒ API

  • query.getResultList()
    • ๊ฒฐ๊ณผ๊ฐ€ ํ•˜๋‚˜ ์ด์ƒ์ผ ๋•Œ, ๋ฆฌ์ŠคํŠธ ๋ฐ˜ํ™˜
    • ๊ฒฐ๊ณผ๊ฐ€ ์—†์œผ๋ฉด ๋นˆ ๋ฆฌ์ŠคํŠธ ๋ฐ˜ํ™˜(NPE ๊ฑฑ์ • ์—†์Œ)
List<Member> resultList = query.getResultList();

 

  • query.getSingleResult()
    • ๊ฒฐ๊ณผ๊ฐ€ ์ •ํ™•ํžˆ ํ•˜๋‚˜, ๋‹จ์ผ ๊ฐ์ฒด ๋ฐ˜ํ™˜
    • ๊ฒฐ๊ณผ๊ฐ€ ์—†์œผ๋ฉด ์˜ˆ์™ธ ๋ฐœ์ƒ javax.persistence.NoResultException
    • ๊ฒฐ๊ณผ๊ฐ€ ๋‘˜ ์ด์ƒ์ด๋ฉด ์˜ˆ์™ธ ๋ฐœ์ƒ javax.persistence.NonUniqueResultException
Member result = query.getSingleResult();

 

 

ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฐ”์ธ๋”ฉ - ์ด๋ฆ„ ๊ธฐ์ค€, ์œ„์น˜ ๊ธฐ์ค€

  • ์ด๋ฆ„๊ธฐ์ค€
Member result = em.createQuery(SELECT m FROM Member m where m.username=:username", Member.class)
.setParameter("username", usernameParam)
.getSingleResult();

 

  •  ์œ„์น˜๊ธฐ์ค€
    • ์ค‘๊ฐ„์— ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ ์—‰ํ‚ฌ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ถŒ์žฅํ•˜์ง€ ์•Š์Œ
SELECT m FROM Member m where m.username=?1 
query.setParameter(1, usernameParam);

 

 

 

ํ”„๋กœ์ ์…˜

  • SELECT ์ ˆ์— ์กฐํšŒํ•  ๋Œ€์ƒ์„ ์ง€์ •ํ•˜๋Š” ๊ฒƒ
  • ํ”„๋กœ์ ์…˜ ๋Œ€์ƒ: ์—”ํ‹ฐํ‹ฐ, ์ž„๋ฒ ๋””๋“œ ํƒ€์ž…, ์Šค์นผ๋ผ ํƒ€์ž…(์ˆซ์ž, ๋ฌธ์ž๋“ฑ ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…)
  • "SELECT m FROM Member m" -> ์—”ํ‹ฐํ‹ฐ ํ”„๋กœ์ ์…˜
    • ์ด ์ฟผ๋ฆฌ๋กœ ์ฐพ๋Š” MemberList๋Š” ๋ชจ๋‘ ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ๋กœ ๊ด€๋ฆฌ ๋จ
  • "SELECT m.team FROM Member m" -> ์—”ํ‹ฐํ‹ฐ ํ”„๋กœ์ ์…˜
    • ์ด ์ฟผ๋ฆฌ๋Š” ์„ฑ๋Šฅ์ด ์ข‹์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์กฐ์ธ์„ ๋ช…์‹œํ•˜๋Š” ์ฟผ๋ฆฌ๋ฅผ ๊ถŒ์žฅํ•จ.
    • "SELECT t FROM Member m JOIN m.team t"
  • "SELECT o.address FROM Order o" -> ์ž„๋ฒ ๋””๋“œ ํƒ€์ž… ํ”„๋กœ์ ์…˜
    • ํ•œ๊ณ„ : SELECT a FROM Address ๋ถˆ๊ฐ€๋Šฅ. ์–ด๋”” ์†Œ์†์ธ์ง€ ์—”ํ‹ฐํ‹ฐ๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ์ •ํ•ด์ค˜์•ผํ•จ.
  • "SELECT m.username, m.age FROM Member m" -> ์Šค์นผ๋ผ ํƒ€์ž… ํ”„๋กœ์ ์…˜
  • DISTINCT๋กœ ์ค‘๋ณต ์ œ๊ฑฐ ๊ฐ€๋Šฅ

ํ”„๋กœ์ ์…˜ - ์—ฌ๋Ÿฌ๊ฐ’ ์กฐํšŒ

  • SELECT m.username, m.age FROM Member m
  • 1. Query ํƒ€์ž…์œผ๋กœ ์กฐํšŒ
  • 2. Object[] ํƒ€์ž…์œผ๋กœ ์กฐํšŒ
  • 3. new ๋ช…๋ น์–ด๋กœ ์กฐํšŒ
    • ๋‹จ์ˆœ ๊ฐ’์„ DTO๋กœ ๋ฐ”๋กœ ์กฐํšŒ
    • "SELECT new jpabook.jpql.UserDTO(m.username, m.age) FROM Member m"
    • ํŒจํ‚ค์ง€ ๋ช…์„ ํฌํ•จํ•œ ์ „์ฒด ํด๋ž˜์Šค ๋ช… ์ž…๋ ฅ์ˆœ์„œ์™€ ํƒ€์ž…์ด ์ผ์น˜ํ•˜๋Š” ์ƒ์„ฑ์ž ํ•„์š”

 

 

ํŽ˜์ด์ง• API

  • JPA๋Š” ํŽ˜์ด์ง•์„ ๋‹ค์Œ ๋‘ API๋กœ ์ถ”์ƒํ™”
  • setFirstResult(int startPosition) : ์กฐํšŒ ์‹œ์ž‘ ์œ„์น˜(0๋ถ€ํ„ฐ ์‹œ์ž‘)
  • setMaxResults(int maxResult) : ์กฐํšŒํ•  ๋ฐ์ดํ„ฐ ์ˆ˜
//ํŽ˜์ด์ง• ์ฟผ๋ฆฌ
 String jpql = "select m from Member m order by m.name desc";
 List<Member> resultList = em.createQuery(jpql, Member.class)
 			.setFirstResult(10)
 			.setMaxResults(20)
 			.getResultList();