# [JPA] JPQL ๋ฌธ๋ฒ• 1 - ๊ธฐ๋ณธ ๋ฌธ๋ฒ•, ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฐ”์ธ๋”ฉ, ํ”„๋กœ์ ์…˜, ํŽ˜์ด์ง•
Study Repository

[JPA] JPQL ๋ฌธ๋ฒ• 1 - ๊ธฐ๋ณธ ๋ฌธ๋ฒ•, ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฐ”์ธ๋”ฉ, ํ”„๋กœ์ ์…˜, ํŽ˜์ด์ง•

by rlaehddnd0422

SQL์ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ํ…Œ์ด๋ธ”์„ ๋Œ€์ƒ์œผ๋กœ ํ•˜๋Š” ์กฐํšŒ ์ฟผ๋ฆฌ ์˜€๋‹ค๋ฉด JPQL์€ ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด๋ฅผ ์กฐํšŒํ•˜๋Š” ๊ฐ์ฒด ์ง€ํ–ฅ ์ฟผ๋ฆฌ์ž…๋‹ˆ๋‹ค.

JPQL์˜ ๋ฌธ๋ฒ•์€ SQL๊ณผ ๋น„์Šทํ•˜๊ณ , ANSI ํ‘œ์ค€ SQL์ด ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์œ ์‚ฌํ•˜๊ฒŒ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

SQL์„ ์ถ”์ƒํ™”ํ•œ ๋•๋ถ„์— ํŠน์ • SQL์— ์˜์กดํ•˜์ง€ ์•Š์•„ ๋Œ€๋ถ€๋ถ„์˜ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

JPQL์€ ๊ฒฐ๊ตญ ๊ฐ์ฒด ์ง€ํ–ฅ์ ์ธ ์ฟผ๋ฆฌ์ผ ๋ฟ, ์‹คํ–‰๋  ๋•Œ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘๊ทผํ•  ๋•Œ์—๋Š” SQL๋กœ ๋ณ€ํ™˜๋˜์–ด ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค.


๊ธฐ๋ณธ ๋ฌธ๋ฒ•๊ณผ ์ฟผ๋ฆฌ API

JPQL๋„ SQL๊ณผ ๋น„์Šทํ•˜๊ฒŒ SELECT, UPDATE, DELETE๋ฌธ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

INSERT๋ฌธ์€ em.persist๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ง์ ‘์ ์œผ๋กœ JPQL์—๋Š” INSERT๋ฌธ์ด ์ง์ ‘์ ์œผ๋กœ ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

 

SELECT ๋ฌธ

select ๋ฌธ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

SELECT m
FROM Member AS m
where m.username='hello'
  • ์—”ํ‹ฐํ‹ฐ์™€ ์†์„ฑ์€ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•ฉ๋‹ˆ๋‹ค. (Member, username)
  • ์—”ํ‹ฐํ‹ฐ ์ด๋ฆ„
    • ์œ„ JPQL์—์„œ ์‚ฌ์šฉํ•œ Member๋Š” ํด๋ž˜์Šค๋ช… X ์—”ํ‹ฐํ‹ฐ๋ช… O
    • ์ž๋ฐ” ์ง„์˜์— ์—”ํ‹ฐํ‹ฐ๋ช…์„ ์ง€์ •(@Entity(name="~")) ํ•˜์ง€ ์•Š์œผ๋ฉด ํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ ์ž๋™ ์ง€์ •๋ฉ๋‹ˆ๋‹ค.
    • ๊ธฐ๋ณธ๊ฐ’์ธ ํด๋ž˜์Šค๋ช…์„ ์—”ํ‹ฐํ‹ฐ๋ช…์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ž…๋‹ˆ๋‹ค.
    • ๋ณ„์นญ : Member AS m์„ ๋ณด๋ฉด Member์— m์ด๋ผ๋Š” ๋ณ„์นญ์„ ๋ถ€์—ฌํ–ˆ์Šต๋‹ˆ๋‹ค. JPQL์—์„œ๋Š” ๋ณ„์นญ์„ ํ•„์ˆ˜์ ์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
      • AS๋Š” ์ƒ๋žต ๊ฐ€๋Šฅ. ๋”ฐ๋ผ์„œ Member m์œผ๋กœ ์ž‘์„ฑํ•ด๋„ ์ข‹์Šต๋‹ˆ๋‹ค.

์ž‘์„ฑํ•œ JPQL๋ฌธ์„ ์‹คํ–‰ํ•˜๋ ค๋ฉด ์ฟผ๋ฆฌ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฟผ๋ฆฌ ๊ฐ์ฒด๋Š” TypeQuery์™€ Query ๊ฐ์ฒด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  • TypeQuery : ๋ฐ˜ํ™˜ํ•  ํƒ€์ž…์„ ๋ช…ํ™•ํ•˜๊ฒŒ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉ
  • Query : ๋ฐ˜ํ™˜ ํƒ€์ž…์„ ๋ช…ํ™•ํ•˜๊ฒŒ ์ง€์ •ํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ ์‚ฌ์šฉ

 

TypeQuery

// ๋ฐ˜ํ™˜ ํƒ€์ž…์„ ๋ช…ํ™•ํ•˜๊ฒŒ ์ง€์ •๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ
TypedQuery<Member> query = em.createQuery("SELECT m FROM Member m", Member.class);
List<Member> resultList = query.getResultList();
for (Member member : resultList) {
    System.out.println("member = " + member);
}
  • ์กฐํšŒ ๋Œ€์ƒ์ด Member ์—”ํ‹ฐํ‹ฐ์ด๋ฏ€๋กœ ์กฐํšŒ ๋Œ€์ƒ ํƒ€์ž…์ด ๋ช…ํ™•ํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ๋Š” TypeQuery๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Query

Query query = em.createQuery("SELECT m.id, m.name FROM Member m");
List resultList = query.getResultList();
for (Object o : resultList) {
    Object[] result = (Object[]) o; // ๊ฒฐ๊ณผ๊ฐ€ ๋‘˜ ์ด์ƒ์ธ ๊ฒฝ์šฐ
    System.out.println("id = " + result[0]);
    System.out.println("name = " + result[1]);
}

  • ์กฐํšŒ ๋Œ€์ƒ์ด String ํƒ€์ž…์ธ ์ด๋ฆ„๊ณผ Integer ํƒ€์ž…์ธ ๋‚˜์ด์ด๋ฏ€๋กœ ์กฐํšŒ ๋Œ€์ƒ ํƒ€์ž…์ด ๋ช…ํ™•ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 
  • ์—ฌ๋Ÿฌ ์—”ํ‹ฐํ‹ฐ๋‚˜ ์ปฌ๋Ÿผ์„ ์„ ํƒํ•  ๋•Œ๋Š” ๋ฐ˜ํ™˜ ํƒ€์ž…์ด ๋ช…ํ™•ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— Query ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ์กฐํšŒ ๋Œ€์ƒ์ด ๋‘˜ ์ด์ƒ์ธ ๊ฒฝ์šฐ Object[]๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์กฐํšŒ ๋Œ€์ƒ์ด ํ•˜๋‚˜์ธ ๊ฒฝ์šฐ Object๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. 
query.getResultList() : ์ฟผ๋ฆฌ ์‹คํ–‰ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ๋ฆฌํ„ด, ๊ฒฐ๊ณผ๊ฐ€ ์—†์œผ๋ฉด ๋นˆ ์ปฌ๋ ‰์…˜ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.
query.getSingleResult() : ๊ฒฐ๊ณผ๊ฐ€ ์ •ํ™•ํžˆ ํ•˜๋‚˜์ผ ๋•Œ ๊ฒฐ๊ณผ ๋ฆฌํ„ด, ๋งŒ์•ฝ ๊ฒฐ๊ณผ๊ฐ€ ์—†๊ฑฐ๋‚˜ ๊ฒฐ๊ณผ๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ์ธ ๊ฒฝ์šฐ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฐ”์ธ๋”ฉ 

JDBC๋Š” ์œ„์น˜ ๊ธฐ๋ฐ˜ (?๋ฅผ ์‚ฌ์šฉํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฐ”์ธ๋”ฉ) ๋ฐ”์ธ๋”ฉ๋งŒ ์ง€์›ํ•˜์ง€๋งŒ JPQL์€ ์ด๋ฆ„ ๊ธฐ์ค€ ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฐ”์ธ๋”ฉ๋„ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.

 

์œ„์น˜ ๊ธฐ๋ฐ˜ ํŒŒ๋ผ๋ฏธํ„ฐ 

List<Member> members = em.createQuery("SELECT m FROM Member m where m.name = ?1", Member.class)
                    .setParameter(1, 'kim')
                    .getResultList();
for (Member member : members) {
    System.out.println("member = " + member);
}

โ€ป JPQL API๋Š” ๋Œ€๋ถ€๋ถ„ ๋ฉ”์†Œ๋“œ ์ฒด์ธ ๋ฐฉ์‹์œผ๋กœ ์„ค๊ณ„๋˜์–ด ์žˆ์–ด ์—ฐ์†ํ•ด์„œ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ด๋ฆ„ ๊ธฐ๋ฐ˜ ํŒŒ๋ผ๋ฏธํ„ฐ

List<Member> members = em.createQuery("SELECT m FROM Member m where m.name := username", Member.class)
                    .setParameter("username", 'kim')
                    .getResultList();
for (Member member : members) {
    System.out.println("member = " + member);
}

์ด๋ฆ„ ๊ธฐ๋ฐ˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„์œผ๋กœ ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋“ฑํ˜ธ ์•ž์— :๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

+ ์ง์ ‘ ๋ฌธ์ž๋ฅผ ๋”ํ•ด JPQL๋ฌธ์„ ๋งŒ๋“œ๋Š” ๋ฐฉ์‹์€ SQL ์ธ์ ์…˜ ๊ณต๊ฒฉ์— ์ƒ๋‹นํžˆ ์ทจ์•ฝํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ๋“ค์–ด ์•„๋ž˜์™€ ๊ฐ™์€ JPQL๋ฌธ์„ ๋งŒ๋“ค์—ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ด…์‹œ๋‹ค.

String query = "SELECT * FROM Member m WHERE m.username = '" + username + "' AND m.password = '" + password + "'";

์ •์ƒ์ ์ธ ์ž…๋ ฅ์˜ ๊ฒฝ์šฐ ์‹คํ–‰๋˜๋Š” JPQL

SELECT * FROM Member m WHERE username = 'john' AND password = 'secret'

์•…์˜์ ์ธ ์ž…๋ ฅ์˜ ๊ฒฝ์šฐ ์‹คํ–‰๋˜๋Š” JPQL

SELECT * FROM Member m WHERE username = '' OR '1'='1' AND password = '' OR '1'='1'

์ด ๊ฒฝ์šฐ, '1'='1' ์กฐ๊ฑด์ด ํ•ญ์ƒ ์ฐธ์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์•…์˜์ ์ธ ์‚ฌ์šฉ์ž๋Š” ๋กœ๊ทธ์ธ ์ ˆ์ฐจ๋ฅผ ์šฐํšŒํ•˜๊ณ  ๋ชจ๋“  ์‚ฌ์šฉ์ž์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋”ฐ๋ผ์„œ ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฐ”์ธ๋”ฉ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋˜, ์ข€ ๋” ๋ช…ํ™•ํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฐ”์ธ๋”ฉ ๋ฐฉ์‹์ธ ์ด๋ฆ„ ๊ธฐ๋ฐ˜ ํŒŒ๋ผ๋ฏธํ„ฐ ๋ฐ”์ธ๋”ฉ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋„๋ก ํ•ฉ์‹œ๋‹ค.


ํ”„๋กœ์ ์…˜ 

select์ ˆ์— ์กฐํšŒํ•  ๋Œ€์ƒ์„ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์„ ํ”„๋กœ์ ์…˜์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

ํ”„๋กœ์ ์…˜์—๋Š” ์—”ํ‹ฐํ‹ฐ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ์ž„๋ฒ ๋””๋“œ ํƒ€์ž…, ์Šค์นผ๋ผ ํƒ€์ž… ๋˜ํ•œ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

'์Šค์นผ๋ผ ํƒ€์ž…'์ด๋ž€?

: ์ˆซ์ž, ๋ฌธ์ž ๋“ฑ ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…

์—”ํ‹ฐํ‹ฐ ํ”„๋กœ์ ์…˜

SELECT m FROM Member m
SELECT m.username FROM Member m

์กฐํšŒํ•œ ์—”ํ‹ฐํ‹ฐ๋Š” ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์—์„œ ๊ด€๋ฆฌ๋ฉ๋‹ˆ๋‹ค.

 

์ž„๋ฒ ๋””๋“œ ํƒ€์ž… ํ”„๋กœ์ ์…˜

์ž„๋ฒ ๋””๋“œ ํƒ€์ž…์ด๋ž€?

 

[JPA] ๊ฐ’ ํƒ€์ž… - ์ž„๋ฒ ๋””๋“œ ๊ฐ’ ํƒ€์ž…, ์ปฌ๋ ‰์…˜ ๊ฐ’ ํƒ€์ž…

JPA์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์—๋Š” ํฌ๊ฒŒ ์—”ํ‹ฐํ‹ฐ ํƒ€์ž…, ๊ฐ’ ํƒ€์ž…์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. // ์—”ํ‹ฐํ‹ฐ ํƒ€์ž… @Entity public class Member{ @Id private Long id; // ๊ฐ’ ํƒ€์ž… private String name; // ๊ฐ’ ํƒ€์ž… @Embedded private Address address; //

rlaehddnd0422.tistory.com

 

JPQL์—์„œ ์ž„๋ฒ ๋””๋“œ ํƒ€์ž…์€ ์—”ํ‹ฐํ‹ฐ์™€ ๊ฑฐ์˜ ๋น„์Šทํ•˜๊ฒŒ ์‚ฌ์šฉ๋˜์ง€๋งŒ, ํ•˜์ง€๋งŒ ํ•œ ๊ฐ€์ง€ ์ œ์•ฝ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹น์—ฐํ•œ ์–˜๊ธฐ์ง€๋งŒ, ์ž„๋ฒ ๋””๋“œ ํƒ€์ž…์€ ์กฐํšŒ์˜ ์‹œ์ž‘์ ์ด ๋  ์ˆ˜๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์‹œ์ž‘์ ์€ ๋ฐ˜๋“œ์‹œ ์—”ํ‹ฐํ‹ฐ๋ฅผ ํ†ตํ•ด์„œ ์ ‘๊ทผํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์ž˜๋ชป๋œ ์˜ˆ์‹œ

SELECT a FROM Address a

์˜ฌ๋ฐ”๋ฅธ ์‚ฌ์šฉ ์˜ˆ์‹œ

TypedQuery<Address> query = em.createQuery("SELECT o.address FROM Orders o", Address.class);
List<Address> resultList = query.getResultList();

์ž„๋ฒ ๋””๋“œ ํƒ€์ž…์€ ์—”ํ‹ฐํ‹ฐ ํƒ€์ž…์ด ์•„๋‹Œ ๊ฐ’ ํƒ€์ž…์ด๊ธฐ ๋–„๋ฌธ์— ์กฐํšŒํ•œ ์ž„๋ฒ ๋””๋“œ ํƒ€์ž…์€ ์˜์†์„ฑ ์ปจํ…์ŠคํŠธ์—์„œ ๊ด€๋ฆฌ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 

 

์Šค์นผ๋ผ ํƒ€์ž… ํ”„๋กœ์ ์…˜

์ˆซ์ž, ๋ฌธ์ž, ๋‚ ์งœ์™€ ๊ฐ™์€ ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…๋“ค์„ ์Šค์นผ๋ผ ํƒ€์ž…์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด ์ „์ฒด ํšŒ์›์˜ ์ด๋ฆ„์„ ์กฐํšŒํ•˜๊ณ  ์‹ถ์„ ๋•Œ ๋‹ค์Œ ์ฒ˜๋Ÿผ ์ฟผ๋ฆฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

TypedQuery<String> query = em.createQuery("SELECT m.name FROM Member m", String.class);
List<String> resultList = query.getResultList();

 

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

์—”ํ‹ฐํ‹ฐ๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์กฐํšŒํ•˜๋ฉด ํŽธ๋ฆฌํ•˜๊ฒ ์ง€๋งŒ, ๊ผญ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋“ค๋งŒ ์„ ํƒํ•ด์„œ ์กฐํšŒํ•ด์•ผ ํ•  ๋•Œ๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ”„๋กœ์ ์…˜์— ์—ฌ๋Ÿฌ๊ฐ’์„ ์„ ํƒํ•˜๋ฉด TypeQuery๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ณ  Query๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

List<Object[]> resultList = em.createQuery("SELECT m.name, m.age FROM Member m")
        .getResultList();
for (Object[] row : resultList) {
    String username = (String) row[0];
    Integer age = (Integer) row[1];

    System.out.println("username = " + username);
    System.out.println("age = " + age);
}

์‹ค์ œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐœ๋ฐœ ์‹œ์—๋Š” Object[]๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  DTO ๋กœ ๋ณ€ํ™˜ํ•ด์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

List<Object[]> resultList = em.createQuery("SELECT m.name, m.age FROM Member m")
        .getResultList();

List<MemberDto> memberDTOs = new ArrayList<MemberDto>();

for (Object[] row : resultList) {
    MemberDto memberDto = new MemberDto((String) row[0], (Integer) row[1]);
    memberDtos.add(memberDto);
}

- DTO ๋ณ€ํ™˜ ์ž‘์—…์„ ์ถ”๊ฐ€๋กœ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

- ๊ฐ์ฒด ๋ณ€ํ™˜ ์ž‘์—…๋„ ๊ท€์ฐฎ๊ฒŒ for๋ฌธ์œผ๋กœ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ , new ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ JPQL ํ•˜๋‚˜๋กœ ํ‰์ณ์„œ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

TypedQuery<MemberDto> query = em.createQuery("SELECT new domain.MemberDto(m.name, m.age) FROM Member m", MemberDto.class);
List<MemberDto> resultList = query.getResultList();
  • new ๋ช…๋ น์–ด ์‚ฌ์šฉ์‹œ ํŒจํ‚ค์ง€ ๋ช…์„ ํฌํ•จํ•œ ์ „์ฒด ํด๋ž˜์Šค๋ช…์„ ์ž…๋ ฅํ•ด์•ผ ํ•˜๊ณ 
  • ์ˆœ์„œ์™€ ํƒ€์ž…์ด ์ผ์น˜ํ•˜๋Š” ์ƒ์„ฑ์ž๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ 

ํŽ˜์ด์ง•์€ ๋ฐ์ดํ„ฐ๋ฅผ ํŽ˜์ด์ง€ ๋‹จ์œ„๋กœ ๋‚˜๋ˆ„์–ด์„œ ์กฐํšŒํ•˜๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋งˆ๋‹ค ํŽ˜์ด์ง•์„ ์ฒ˜๋ฆฌํ•˜๋Š” SQL๋ฌธ๋ฒ•์ด ๋‹ค๋ฅด์ง€๋งŒ, JPA๋Š” ํŽ˜์ด์ง•์„ ๋‹ค์Œ ๋‘ API๋กœ ์ถ”์ƒํ™” ํ•ด๋‘์—ˆ์Šต๋‹ˆ๋‹ค.

  • setFirstResult(int startPosition) : ์กฐํšŒ ์‹œ์ž‘ ์œ„์น˜ ( 0๋ถ€ํ„ฐ ์‹œ์ž‘ )
  • setMaxResults(int maxResult) : ์กฐํšŒํ•  ๋ฐ์ดํ„ฐ ์ˆ˜ 

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

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

Study Repository

rlaehddnd0422

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