📘
Kotlin JDSL
Discord
en
en
  • Kotlin JDSL
  • JPQL with Kotlin JDSL
    • Statements
    • Entities
    • Paths
    • Expressions
    • Predicates
    • Sorts
    • Subqueries
    • Custom DSL
    • Spring supports
    • Migration 2.X to 3.X
  • Kotlin JDSL Roadmap
  • FAQ
    • How can I see the generated query?
    • How can I use Kotlin value class?
    • What is the difference between Kotlin JDSL and jOOQ and QueryDSL?
    • Why is there a support module that only has a nullable return type
Powered by GitBook
On this page
  • Spring Boot AutoConfigure
  • Spring Data Repository
  • Spring Batch

Was this helpful?

Edit on GitHub
  1. JPQL with Kotlin JDSL

Spring supports

Spring Boot AutoConfigure

Kotlin JDSL supports Spring Boot AutoConfigure. If your project has both Spring Boot and com.linecorp.kotlin-jdsl:spring-data-jpa-support or com.linecorp.kotlin-jdsl:spring-batch-support dependencies, AutoConfiguration automatically creates the JpqlRenderContext bean.

If you declare your JpqlSerializer or JpqlIntrospector as a bean, it will be included with the JpqlRenderContext bean.

Spring Data Repository

If your JpaRepository extends KotlinJdslJpqlExecutor, you can use the extension provided by Kotlin JDSL.

interface BookRepository : JpaRepository<Book, Isbn>, KotlinJdslJpqlExecutor

val result: List<Isbn?> = bookRepository.findAll {
    select(
        path(Book::isbn),
    ).from(
        entity(Book::class),
    )
}

val result: Page<Isbn?> = bookRepository.findPage(pageable) {
    select(
        path(Book::isbn),
    ).from(
        entity(Book::class),
    )
}

val result: Slice<Isbn?> = bookRepository.findSlice(pageable) {
    select(
        path(Book::isbn),
    ).from(
        entity(Book::class),
    )
}

val result: Stream<Isbn?> = bookRepository.findStream {
    select(
        path(Book::isbn),
    ).from(
        entity(Book::class),
    )
}

If you want to use KotlinJdslJpqlExecutor in @DataJpaTest, you need to import KotlinJdslAutoConfiguration in the test. Since @DataJpaTest is a slice test, it only creates minimal beans. And the minimal bean does not include KotlinJdslAutoConfiguration. So if you want to use the features of Kotlin JDSL in @DataJpaTest, you need to import KotlinJdslAutoConfiguration directly in your test.

Spring Batch

Spring Batch provides JpaPagingItemReader and JpaCursorItemReader for querying data with JPQL. Kotlin JDSL provides KotlinJdslQueryProvider so that a JPQL query created in DSL can be executed in it.

@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()
PreviousCustom DSLNextMigration 2.X to 3.X

Last updated 5 months ago

Was this helpful?