📘
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
  • Requirements
  • Configure the repositories
  • Release
  • Snapshot
  • Add Kotlin JDSL dependencies
  • Core dependencies
  • Support dependencies
  • Build a query
  • Execute the query

Was this helpful?

Edit on GitHub

JPQL with Kotlin JDSL

PreviousKotlin JDSLNextStatements

Last updated 2 months ago

Was this helpful?

Requirements

At least Java 8 and Kotlin 1.7 are required to build and run an application with Kotlin JDSL.

Configure the repositories

Before adding Kotlin JDSL dependencies, you need to configure the repositories for this project:

Release

Releases of Kotlin JDSL are available in the . You can declare this repository in your build script as follows:

repositories {
    mavenCentral()
}
repositories {
    mavenCentral()
}

You don't need to add the Maven central repository in the pom.xml file since your project inherits the central repository from .

Snapshot

Declare the OSS snapshot repository to get access to snapshots of Kotlin JDSL:

repositories {
    maven(url = "https://oss.sonatype.org/content/repositories/snapshots")
}
repositories {
    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}
<repositories>
    <repository>
        <id>oss.sonatype.org-snapshot</id>
        <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
    </repository>
</repositories>

Add Kotlin JDSL dependencies

Core dependencies

The following dependencies are the minimum requirement for all Kotlin JDSL applications:

  • jpql-dsl: Contain a DSL to build a JPQL query

  • jpql-render: Render the JPQL query created with the DSL as String

dependencies {
    implementation("com.linecorp.kotlin-jdsl:jpql-dsl:3.5.5")
    implementation("com.linecorp.kotlin-jdsl:jpql-render:3.5.5")
}
dependencies {
    implementation 'com.linecorp.kotlin-jdsl:jpql-dsl:3.5.5'
    implementation 'com.linecorp.kotlin-jdsl:jpql-render:3.5.5'
}

<dependencies>
    <dependency>
        <groupId>com.linecorp.kotlin-jdsl</groupId>
        <artifactId>jpql-dsl</artifactId>
        <version>3.5.5</version>
    </dependency>
    <dependency>
        <groupId>com.linecorp.kotlin-jdsl</groupId>
        <artifactId>jpql-render</artifactId>
        <version>3.5.5</version>
    </dependency>
</dependencies>

Support dependencies

Add the Support dependencies to execute the query created with the DSL. For each JPA provider, Kotlin JDSL provides the following dependencies:

  • hibernate-support: Assist to execute the query with Hibernate

  • eclipselink-support: Assist to execute the query with EclipseLink

  • spring-batch-support: Assist to execute the query with Spring Batch

  • spring-data-jpa-support: Assist to execute the query with Spring Data JPA

  • hibernate-reactive-support: Assist to execute the query with Hibernate Reactive

Javax

For the javax, Kotlin JDSL also provides the following dependencies:

  • hibernate-javax-support: Assist to execute the query with Hibernate

  • eclipselink-javax-support: Assist to execute the query with EclipseLink

  • spring-batch-javax-support: Assist to execute the query with Spring Batch.

  • spring-data-jpa-javax-support: Assist to execute the query with Spring Data JPA.

  • hibernate-reactive-javax-support: Assist to execute the query with Hibernate Reactive

Build a query

val query = jpql {
    select(
        path(Author::authorId),
    ).from(
        entity(Author::class)
    )
}

Execute the query

After building the query, you can use RenderContext to execute the query. For example, you can use JpqlRenderContext to execute the query:

val context = JpqlRenderContext()

val renderer = JpqlRenderer()

val rendered = renderer.render(query, context)

val jpaQuery: Query = entityManager.createQuery(rendered.query).apply {
    rendered.params.forEach { (name, value) ->
        setParameter(name, value)
    }
}

val result = jpaQuery.resultList

RenderContext has elements for rendering the query as String. Kotlin JDSL provides JpqlRenderContext as the default RenderContext for the JPQL.

JpqlRenderer renders the query as String using the RenderContext. This returns JpqlRendered, which has the query rendered as String and the parameters contained in the query. This has no state, so you can reuse the object of this and access it from multiple threads.

Creating RenderContext is expensive, so the Kotlin JDSL recommends creating it once and reusing it afterward. Since RenderContext is immutable, you can access RenderContext from multiple threads.

val context = JpqlRenderContext()

val jpaQuery: Query = entityManager.createQuery(query, context)

val result = jpaQuery.resultList

You can call select() in jpql() to build a :

Similarly, Kotlin JDSL provides functions for all the other statements: , and . You can also see more on GitHub.

In addition, you can also create your own

provides extension functions for EntityManager to simplify the above process. Using them, you can execute queries as simple as:

Maven central repository
Super POM
custom DSL
Kotlin JDSL Support
examples
select statement
update statement
delete statement