Kotlin JDSL์ ๋ฉํ๋ชจ๋ธ ์์ด ์ฟผ๋ฆฌ๋ฅผ ์ฝ๊ฒ ๋ง๋ค ์ ์๊ฒ ๋์์ฃผ๋ Kotlin ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค. ์ด๋ฏธ Annotation Processor Tool (APT)์ ์ด์ฉํด์ ์ฟผ๋ฆฌ๋ฅผ ์ฝ๊ฒ ๋ง๋ค๋๋ก ๋์์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ๋ง์ด ์์ต๋๋ค. ํ์ง๋ง APT๋ฅผ ์ด์ฉํ๋ฉด ํด๋์ค๋ช
์ด๋ ํ๋๋ช
์ด ๋ณ๊ฒฝ๋์์ ๋๋ง๋ค ๋ค์ ์ปดํ์ผ ํด์ผ ํ๋ ๋ถํธํจ์ด ์์ต๋๋ค. Kotlin JDSL์ ์ด๋ฐ ๋ถํธํจ์ ํด์ํ๋ฉด์ ์ฝ๊ฒ ์ฟผ๋ฆฌ๋ฅผ ๋ง๋ค ์ ์๋๋ก KClass์ KProperty ๊ธฐ๋ฐ์ Domain-Specific Language๋ฅผ ์ ๊ณตํฉ๋๋ค.
Kotlin JDSL์ ์๋ก์ด DB ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์๋ DB ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ ๋ ๋์์ ์ฃผ๊ธฐ ์ํด์ ๋ง๋ค์ด์ง ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๊ธฐ ๋๋ฌธ์ ์ง์ ์ฟผ๋ฆฌ๋ฅผ ์คํํ๊ฑฐ๋ DB ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ํํ๋ ๋ฑ์ ์์
์ ํ์ง ์์ต๋๋ค.
val context = JpqlRenderContext()
val query = jpql {
select(
path(Author::authorId),
).from(
entity(Author::class),
join(BookAuthor::class).on(path(Author::authorId).equal(path(BookAuthor::authorId))),
).groupBy(
path(Author::authorId),
).orderBy(
count(Author::authorId).desc(),
)
}
val `the most prolific author` = entityManager.createQuery(query, context).apply {
maxResults = 1
}