Spring supports

Spring Boot AutoConfigure

Kotlin JDSL์€ Spring Boot AutoConfigure๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ”„๋กœ์ ํŠธ๊ฐ€ Spring Boot์™€ com.linecorp.kotlin-jdsl:spring-data-jpa-support dependency๋ฅผ ๊ฐ™์ด ํฌํ•จํ•˜๊ณ  ์žˆ๋‹ค๋ฉด, JpqlRenderContext bean์ด KotlinJdslAutoConfiguration์„ ํ†ตํ•ด ์ž๋™ ์ƒ์„ฑ ๋ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ JpqlSerializer ๋˜๋Š” JpqlIntrospector๋ฅผ bean์œผ๋กœ ์„ ์–ธํ–ˆ๋‹ค๋ฉด, ์ž๋™์œผ๋กœ JpqlRenderContext์— ํ•ด๋‹น bean์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.

Spring Data Repository

๋งŒ์•ฝ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” JpaRepository๊ฐ€ KotlinJdslJpqlExecutor๋ฅผ ์ƒ์†ํ•˜๋ฉด, Kotlin JDSL์ด ์ œ๊ณตํ•˜๋Š” ํ™•์žฅ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

interface AuthorRepository : JpaRepository<Author, Long>, KotlinJdslJpqlExecutor
interface BookRepository : JpaRepository<Book, Isbn>, KotlinJdslJpqlExecutor

authorRepository.findAll {
    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(),
    )
}

bookRepository.findPage(pageable) {
    select(
        path(Book::isbn),
    ).from(
        entity(Book::class),
    )
}

๋งŒ์•ฝ KotlinJdslJpqlExecutor๋ฅผ @DataJpaTest์—์„œ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด KotlinJdslAutoConfiguration๋ฅผ ํ…Œ์ŠคํŠธ์—์„œ ์ง์ ‘ import ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. @DataJpaTest๋Š” slice test์ด๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์†Œํ•œ์˜ bean๋งŒ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด bean์—๋Š” KotlinJdslAutoConfiguration์ด ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ @DataJpaTest์—์„œ Kotlin JDSL์˜ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ํ…Œ์ŠคํŠธ์—์„œ KotlinJdslAutoConfiguration๋ฅผ ์ง์ ‘ import ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

Spring Batch

SpringBatch๋Š” JPQL๋กœ ์ฟผ๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋„๋ก JpaPagingItemReader์™€ JpaCursorItemReader๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. Kotlin JDSL์€ DSL๋กœ ์ƒ์„ฑ๋œ JPQL ์ฟผ๋ฆฌ๊ฐ€ ์œ„ ItemReader๋“ค์—์„œ ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋„๋ก KotlinJdslQueryProvider๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

@Auwoired
lateinit var queryProviderFactory: KotlinJdslQueryProviderFactory

val queryProvider = queryProviderFactory.create {
    select(
        path(Book::isbn)
    ).from(
        entity(Book::class),
    )
}

JpaCursorItemReaderBuilder<Isbn>()
    .entityManagerFactory(entityManagerFactory)
    .queryProvider(queryProvider)
    .saveState(false)
    .build()

Last updated