[Querydsl] Q-ํ์ (Q-type)
๐ ๋ค์ด๊ฐ๋ฉฐ
์ด๋ฒ ๊ธ์์๋ QueryDSL์ ํต์ฌ์ธ Q-Type์ ์ ๋ฆฌํ๋ค.
QueryDSL์ด๋? ์๋ฐ ๊ธฐ๋ฐ์ ์คํ์์ค SQL ํ๋ก์ ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก JPAยทHibernate๋ฅผ ์ง์ํ๋ค. Q-Type์ด๋? ์ํฐํฐ์ ํ๋๋ฅผ ์ ์ ์์๋ก ์ ์ธํ ํด๋์ค. ์ฟผ๋ฆฌ ์์ฑ ์ ํ์ ์์ ์ฑ์ ์ ๊ณตํ๋ค.
1. Q-Type ์ฌ์ฉํ๊ธฐ
Product ์ํฐํฐ๊ฐ ์๋ค๊ณ ํ์.
1
2
3
4
5
6
7
8
@Entity
public class Product {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private int price;
private String category;
}
์๋ ์์ฑ๋ QProduct.product๋ก ํ์
์์ ํ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํ๋ค.
1
2
3
4
5
6
7
8
import static com.example.model.QProduct.product;
public List<Product> findProductsInCategory(String category) {
return new JPAQueryFactory(entityManager)
.selectFrom(product)
.where(product.category.eq(category)) // ํ๋ ์ ๊ทผ ์ ์คํ ๋ฐฉ์ง
.fetch();
}
๐ก
product.category.eq(category)์ฒ๋ผ ํ๋์ ์ ๊ทผํ๋ฉด, ๋ฌธ์์ด JPQL๊ณผ ๋ฌ๋ฆฌ ์ปดํ์ผ ์์ ์ ์คํ๋ฅผ ์ก์์ค๋ค. ์ด๊ฒ์ด QueryDSL์ ๊ฐ์ฅ ํฐ ์ฅ์ ์ธ ํ์ ์์ ์ฑ์ด๋ค.
| ๋ฐฉ์ | ์คํ ๊ฒ์ถ |
|---|---|
| ๋ฌธ์์ด JPQL | ๋ฐํ์์ ๋ฐ๊ฒฌ |
| Q-Type | ์ปดํ์ผ ์์ ์ ๋ฐ๊ฒฌ |
๐ ์ฃผ์์ฌํญ๊ณผ ํ
| ํญ๋ชฉ | ๋ด์ฉ |
|---|---|
| ๋น๋ ์ ์์ฑ ํ์ธ | Q-Type์ ์ฝ๋ ์์ฑ์ผ๋ก ๋ง๋ค์ด์ง โ ๋น๋ ํ ํ์ธ |
| ์ํฐํฐ์ ์ผ์น | Q-Type ํ๋๋ ์ํฐํฐ ํ๋์ ์ผ์นํด์ผ ํจ |
| ์๋ ์์ฑ ํ์ฉ | IDE ์๋์์ฑ์ผ๋ก ๋น ๋ฅด๊ณ ์ ํํ๊ฒ |
๐ ์ ๋ฆฌ
1
2
3
4
5
QueryDSL Q-Type
โโ ์ ์ฒด ์ํฐํฐ ํ๋๋ฅผ ์์๋ก ์ ์ธํ ํด๋์ค
โโ ์ฌ์ฉ QProduct.product.category.eq(...)
โโ ์ฅ์ ํ์
์์ ์ฑ (์ปดํ์ผ ์ ์คํ ๊ฒ์ถ)
โโ ์์ฑ ๋น๋ ์ ์๋ ์์ฑ
| ๊ฐ๋ | ํ ์ค ์ ์ |
|---|---|
| QueryDSL | ํ์ ์์ ํ ์ฟผ๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ |
| Q-Type | ์ํฐํฐ ํ๋๋ฅผ ์์ํํ ํด๋์ค |
| ํ์ ์์ ์ฑ | ์ปดํ์ผ ์์ ์คํ ๊ฒ์ถ |
Q-Type์ QueryDSL์ ์ฌ์ฅ์ด๋ค. ๋ฌธ์์ด ์ฟผ๋ฆฌ์ ์คํ ์ํ์ ์ปดํ์ผ ์์ ์ ์ ๊ฑฐํด์ฃผ๋, ๋ณต์กํ ๋์ ์ฟผ๋ฆฌ๋ ์์ ํ๊ฒ ์์ฑํ ์ ์๋ค.
Comments powered by Disqus.