Querydsl: selfJoin์ด ์ฟผ๋ฆฌ์— ์žˆ์„ ๋•Œ ๊ฐ€์ ธ์˜ค๊ธฐ ๋ฌธ์ œ

์— ๋งŒ๋“  2019๋…„ 04์›” 25์ผ  ยท  4์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: querydsl/querydsl

ํ…Œ์ด๋ธ”์— ์ž์ฒด ์กฐ์ธ์ด ์žˆ๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
์ฟผ๋ฆฌํŒฉํ† ๋ฆฌ
.select(queryBase.getColumns())
.from(entityPath)
.leftJoin(QTodoEntity.todoEntity.parentLocation, QTodoEntity.todoEntity)
.leftJoin(QTodoEntity.locationEntity.country, QCountryEntity.countryEntity)
.where(queryBase.getFilters())
.orderBy(queryBase.getOrderBy())
.offset(queryBase.getOffset())
.limit(queryBase.getLimit())
.์ˆ ์ฑ…()
.๊ฐœ์šธ()
.map(queryBase::mapRow)
.collect(์ˆ˜์ง‘๊ธฐ.toList());

์™ผ์ชฝ ์กฐ์ธ์—์„œ ์ƒ์„ฑ๋œ QTodoEntity์˜ ๋ณ„์นญ์€ ์ฟผ๋ฆฌ์˜ ๋ชจ๋“  ๊ณณ์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์„ ํƒํ•  ๋•Œ ์ƒ์„ฑ๋œ ๋ณ„์นญ์„ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
์ฟผ๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

todo1.id, todo1.name, todo1.custom, todo1.selfChild ์„ ํƒ todo todoOn ์™ผ์ชฝ ์™ธ๋ถ€ ์กฐ์ธ todoOnSelect.selfChild=todo1.id์—์„œ Todo todo1 ์„ ํƒ

ํ•˜์ง€๋งŒ ์ฟผ๋ฆฌ๋Š” -
todoOnSelect.id, todoOnSelect.name, todoOnSelect.custom, todoOnSelect.selfChild๋ฅผ Todo์—์„œ ์„ ํƒ todoOnSelect ์™ผ์ชฝ ์™ธ๋ถ€ ์กฐ์ธ todo todo1 on todoOnSelect.selfChild=todo1.id

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹œ๋„ํ•˜์‹ญ์‹œ์˜ค -
QCategory ์นดํ…Œ๊ณ ๋ฆฌ = QCategory.category;
QCategory subCategory = ์ƒˆ๋กœ์šด QCategory("ํ•˜์œ„");
query.from(์นดํ…Œ๊ณ ๋ฆฌ)
.leftJoin(category.subcategory, subCategory)
.where(category.type.equalsIgnoreCase("A"))
.fetchCount();

์ด๊ฒƒ์€ subCategory์— ๋ณ„์นญ์„ ์ œ๊ณตํ•˜๊ณ  where ์ ˆ์—์„œ ์™ผ์ชฝ ์กฐ์ธ๋œ ํ•ญ๋ชฉ์ด ์•„๋‹Œ ๊ธฐ๋ณธ ํ•ญ๋ชฉ์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

๋ชจ๋“  4 ๋Œ“๊ธ€

์ด๊ฒƒ์€ ๋ฒ„๊ทธ๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.

์ด๊ฒƒ์€ ๋ฒ„๊ทธ๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.

์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•˜์…จ๋‚˜์š”? ๋™์ผํ•œ ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š”๋ฐ where ์ ˆ์ด 'from' ๋ณ„์นญ ๋Œ€์‹  'left join' ๋ณ„์นญ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์œผ๋ฉฐ ๊ฒฐ๊ณผ๊ฐ€ ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

๋‚˜๋Š” ์–ป๊ณ ์žˆ๋‹ค :
select count(sub.id) from category c
left join category sub on c.id = sub.id
where sub.type='A'

๊ทธ๊ฒƒ์€ํ•ด์•ผํ•œ๋‹ค:
select count(c.id) from category c
left join category sub on c.id = sub.id
where c.type='A'

๋‹ค์Œ์€ queryDsl์ž…๋‹ˆ๋‹ค.
QCategory category = QCategory.category;
query.from(category)
.leftJoin(category.subcategory, QCategory.category)
.where(category.type.equalsIgnoreCase("A"))
.fetchCount();
๊ฐ์‚ฌ ํ•ด์š”

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹œ๋„ํ•˜์‹ญ์‹œ์˜ค -
QCategory ์นดํ…Œ๊ณ ๋ฆฌ = QCategory.category;
QCategory subCategory = ์ƒˆ๋กœ์šด QCategory("ํ•˜์œ„");
query.from(์นดํ…Œ๊ณ ๋ฆฌ)
.leftJoin(category.subcategory, subCategory)
.where(category.type.equalsIgnoreCase("A"))
.fetchCount();

์ด๊ฒƒ์€ subCategory์— ๋ณ„์นญ์„ ์ œ๊ณตํ•˜๊ณ  where ์ ˆ์—์„œ ์™ผ์ชฝ ์กฐ์ธ๋œ ํ•ญ๋ชฉ์ด ์•„๋‹Œ ๊ธฐ๋ณธ ํ•ญ๋ชฉ์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‹œ๋„ํ•˜์‹ญ์‹œ์˜ค -
QCategory ์นดํ…Œ๊ณ ๋ฆฌ = QCategory.category;
QCategory subCategory = ์ƒˆ๋กœ์šด QCategory("ํ•˜์œ„");
query.from(์นดํ…Œ๊ณ ๋ฆฌ)
.leftJoin(category.subcategory, subCategory)
.where(category.type.equalsIgnoreCase("A"))
.fetchCount();

์ด๊ฒƒ์€ subCategory์— ๋ณ„์นญ์„ ์ œ๊ณตํ•˜๊ณ  where ์ ˆ์—์„œ ์™ผ์ชฝ ์กฐ์ธ๋œ ํ•ญ๋ชฉ์ด ์•„๋‹Œ ๊ธฐ๋ณธ ํ•ญ๋ชฉ์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๊ฒƒ์ด ์ž‘๋™ํ•˜๋Š” ์ด์œ ๋Š” ํ™•์‹คํ•˜์ง€ ์•Š์ง€๋งŒ ํ™•์‹คํžˆ ํŠธ๋ฆญ์„ ์ˆ˜ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค.
์ •๋ง ๊ณ ๋ง™์Šต๋‹ˆ๋‹ค!
:)

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰