Search
Duplicate
๐Ÿงต

ํ”„๋กœ์ ์…˜

select ํ”„๋กœ์ ์…˜์— ์ปฌ๋Ÿผ์ด ํ•˜๋‚˜ ๋˜๋Š” ํ…Œ์ด๋ธ” ์ „์ฒด๋ผ๋ฉด ํ•ด๋‹น ํƒ€์ž…์œผ๋กœ ์กฐํšŒ ๊ฐ€๋Šฅ
์ปฌ๋Ÿผ์ด ๋‘˜ ์ด์ƒ์ด๋ฉด Tuple ๋˜๋Š” Dto ๋กœ ์กฐํšŒํ•ด์•ผํ•จ

Tuple

querydsl.core ํŒจํ‚ค์ง€
์˜์†ํ™” ๊ณ„์ธต๊นŒ์ง€๋งŒ, ์„œ๋น„์Šค ๊ณ„์ธต๊นŒ์ง€ ๋„˜์–ด๊ฐ€๋ฉด ์ข‹์€ ์„ค๊ณ„๊ฐ€ ์•„๋‹ˆ๋‹ค. ์˜์กด์„ฑ ๋ฐฉํ–ฅ ์ธก๋ฉด์—์„œ ํ•˜๋ถ€ ๊ณ„์ธต์ด ์ƒ๋ถ€ ๊ณ„์ธต์— ์ข…์†๋˜๊ธฐ๋•Œ๋ฌธ

Dto

โ€ข
JPQL
JPQL ์—์„œ๋Š” ์ƒ์„ฑ์ž๋กœ ๋งŒ๋“ค์–ด์ฃผ์–ด์•ผ ํ•˜๋Š” ๋ถˆํŽธํ•จ์ด ์žˆ๋‹ค. ๋˜ํ•œ ์ƒ์„ฑ์ž ๋ฐฉ์‹๋งŒ ์ง€์›ํ•œ๋‹ค.
select new com.example.dto.MemberDto(m.username, m.age) from Member m
โ€ข
setter
getter, setter, ๊ธฐ๋ณธ ์ƒ์„ฑ์ž ํ•„์š”
select(Projections.bean(MemberDto.class, member.username, member.age))
โ€ข
field ์ ‘๊ทผ ๋ฐฉ์‹
getter, setter ๋ถˆํ•„์š”
select(Projections.field(MemberDto.class, member.username, member.age))
โ€ข
constructor ๋ฐฉ์‹
select(Projections.constructor(MemberDto.class, member.username, member.age))
โ€ข
Q ํƒ€์ž… ๋ฐฉ์‹
Dto ์— @QueryProjection ์„ ํ•˜๋ฉด dto ๋„ Q ํƒ€์ž… ๊ฐ์ฒด๋กœ ์ƒ์„ฑ๋œ๋‹ค.
select(new QMemberDto(member.username, member.age))
constructor ๋ฐฉ์‹๊ณผ ๋‹ค๋ฅธ์ :
constructor ๋ฐฉ์‹์€ dto ํ•„๋“œ๊ฐ€ ๋ณ€๊ฒฝ๋˜์—ˆ์„ ๋•Œ ๋ณ€๊ฒฝ์ ์„ compile ๋‹จ๊ณ„์—์„œ ์žก์„ ์ˆ˜ ์—†๋‹ค. ์ธ์ž๊ฐ€ ๊ฐ€๋ณ€์ธ์ž๊ธฐ๋•Œ๋ฌธ์—!
์˜์กด ๋ฐฉํ–ฅ์— ๋Œ€ํ•œ ๋ฌธ์ œ์ ์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค.