๐ ๋ค์ด๊ฐ๋ฉฐ
์ด๋ฒ ๊ธ์์๋ QueryDSL์์ ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ ธ์ค๋ fetch ๊ณ์ด ๋ฉ์๋๋ค์ ์ ๋ฆฌํ๋ค. ์ํฉ์ ๋ง๋ fetch๋ฅผ ๊ณจ๋ผ ์ฐ๋ ๊ฒ์ด ์ค์ํ๋ค.
1. fetch ๋ฉ์๋์ ์ข
๋ฅ
| ๋ฉ์๋ | ๋ฐํ | ํน์ง |
|---|
fetch() | List<T> | ๊ธฐ๋ณธ, ์ฌ๋ฌ ๊ฒฐ๊ณผ |
fetchOne() | T | ๋จ์ผ ๊ฒฐ๊ณผ (์๊ฑฐ๋ ์ฌ๋ฟ์ด๋ฉด ์์ธ) |
fetchFirst() | T | ์ฒซ ๊ฒฐ๊ณผ (์์ผ๋ฉด null) |
fetchResults() | QueryResults<T> | ๊ฒฐ๊ณผ + ์ ์ฒด ๊ฐ์ (ํ์ด์ง) |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| // fetch() โ ๋ฆฌ์คํธ
List<Product> products = queryFactory
.selectFrom(product).where(product.price.gt(1000)).fetch();
// fetchOne() โ ๋จ์ผ (0๊ฐ/2๊ฐโ ์ ์์ธ)
Product one = queryFactory
.selectFrom(product).where(product.id.eq(1L)).fetchOne();
// fetchFirst() โ ์ฒซ ๊ฒฐ๊ณผ (์์ผ๋ฉด null)
Product first = queryFactory
.selectFrom(product).where(product.category.eq("Electronics")).fetchFirst();
// fetchResults() โ ๊ฒฐ๊ณผ + ์ ์ฒด ๊ฐ์
QueryResults<Product> results = queryFactory
.selectFrom(product).where(product.price.between(500, 1000)).fetchResults();
List<Product> list = results.getResults();
long total = results.getTotal();
|
๐ ์ฌ์ฉ๋ฒ๊ณผ ์ฃผ์์ฌํญ
| ์ํฉ | ์ ํ |
|---|
| ์ฌ๋ฌ ๊ฒฐ๊ณผ | fetch() |
| ๋ฐ๋์ ํ๋ | fetchOne() (์์ธ ์ฃผ์) |
| ์์ ์๋ ์์ ์๋ | fetchFirst() |
| ํ์ด์ง (์ ์ฒด ๊ฐ์ ํ์) | fetchResults() |
โ ๏ธ fetch๋ ๋ฐ์ดํฐ๋ฅผ ๋ฉ๋ชจ๋ฆฌ๋ก ๋ก๋ฉํ๋ฏ๋ก ๋๋ ๋ฐ์ดํฐ๋ ์ฑ๋ฅ์ ๊ณ ๋ คํด์ผ ํ๋ค. ์ฐ๊ด ์ํฐํฐ๋ฅผ ํจ๊ป ๋ก๋ฉํ๋ ค๋ฉด join(fetch join)์ ํ์ฉํ๋ค.
๐ ์ ๋ฆฌ
1
2
3
4
5
| QueryDSL fetch
โโ fetch() List ๋ฐํ
โโ fetchOne() ๋จ์ผ (์๊ฑฐ๋ ์ฌ๋ฟ โ ์์ธ)
โโ fetchFirst() ์ฒซ ๊ฒฐ๊ณผ (์์ผ๋ฉด null)
โโ fetchResults() ๊ฒฐ๊ณผ + ์ ์ฒด ๊ฐ์ (ํ์ด์ง)
|
| ๋ฉ์๋ | ํ ์ค ์ ์ |
|---|
| fetch | ์ฌ๋ฌ ๊ฒฐ๊ณผ ๋ฆฌ์คํธ |
| fetchOne | ์ ํํ ํ๋ |
| fetchFirst | ์ฒซ ๊ฒฐ๊ณผ(์์ผ๋ฉด null) |
| fetchResults | ๊ฒฐ๊ณผ+๊ฐ์ |
fetch ๋ฉ์๋๋ โ๊ฒฐ๊ณผ๊ฐ ๋ช ๊ฐ์ธ๊ฐ, ๊ฐ์๊ฐ ํ์ํ๊ฐโ์ ๋ฐ๋ผ ๊ณจ๋ผ ์ด๋ค. ํนํ fetchOne()์ ๊ฒฐ๊ณผ๊ฐ 0๊ฐ๋ 2๊ฐ ์ด์์ด๋ฉด ์์ธ๊ฐ ๋๋ฏ๋ก, ํ์คํ ๋๋ง ์ฌ์ฉํ์.
Comments powered by Disqus.