JPA Specification Kotlin DSL

A Kotlin DSL for building type-safe JPA queries using idiomatic Kotlin syntax. The library provides three distinct DSLs:
- Predicate DSL - Build JPA
Predicate objects for use within Specification or PredicateSpecification
- Specification DSL - Build complete
Specification<T> queries
- PredicateSpecification DSL - Build complete
PredicateSpecification<T> queries
This is achieved with zero runtime overhead by providing extension functions on Kotlin property references
KProperty1<T, P>, letting you write type-safe predicates directly from property references.
Features
- Three DSL flavors:
- Predicate DSL - For building predicates to use within
Specification {} or PredicateSpecification {}
- Specification DSL - Complete specifications
- PredicateSpecification DSL - Complete predicate specifications
- Type-safe queries using Kotlin property references (
::) - implemented as extension functions on
KProperty1<T, ?>
- Idiomatic Kotlin syntax with infix operators
- Composable specifications with
and and or operators
- Zero overhead - extension functions on top of JPA (no proxies or runtime code generation)
- Mix and match - Use predicates within specifications or use specifications standalone
Before and after
Below is a short comparison showing how a simple specification looks with plain Spring Data JPA (without the DSL) and
how the same logic becomes concise using this Kotlin DSL.
Without the DSL:
val spec = Specification<User> { root, query, cb ->
val namePath = root.get<String>("name")
val agePath = root.get<Int>("age")
cb.and(
cb.equal(namePath, "Alice"),
cb.greaterThan(agePath, 18)
)
}
repository.findAll(spec)
With the DSL:
repository.findAll(User::name.equal("Alice") and User::age.greaterThan(18))
Modules
Core (jpa-spec-kotlin-dsl)
The standard DSL - works with any JPA provider. Provides all predicate, specification, and predicate-specification
extensions for equality, comparison, inclusion, nullability, string, collection, join, fetch, and bool operations.
Hibernate extensions (jpa-spec-kotlin-dsl-hibernate)
Hibernate-specific extensions that build on the core DSL.
Requires the core module to also be declared as a dependency.
Installation
Core module only
Gradle (Kotlin DSL)
dependencies {
implementation("io.github.alfonsoristorato:jpa-spec-kotlin-dsl:<version>")
}
Gradle (Groovy)
dependencies {
implementation 'io.github.alfonsoristorato:jpa-spec-kotlin-dsl:<version>'
}
Maven
<dependency>
<groupId>io.github.alfonsoristorato</groupId>
<artifactId>jpa-spec-kotlin-dsl</artifactId>
<version>version</version>
</dependency>
Core + Hibernate extensions
Gradle (Kotlin DSL)
dependencies {
implementation("io.github.alfonsoristorato:jpa-spec-kotlin-dsl:<version>")
implementation("io.github.alfonsoristorato:jpa-spec-kotlin-dsl-hibernate:<version>")
}
Gradle (Groovy)
dependencies {
implementation 'io.github.alfonsoristorato:jpa-spec-kotlin-dsl:<version>'
implementation 'io.github.alfonsoristorato:jpa-spec-kotlin-dsl-hibernate:<version>'
}
Maven
<dependency>
<groupId>io.github.alfonsoristorato</groupId>
<artifactId>jpa-spec-kotlin-dsl</artifactId>
<version>version</version>
</dependency>
<dependency>
<groupId>io.github.alfonsoristorato</groupId>
<artifactId>jpa-spec-kotlin-dsl-hibernate</artifactId>
<version>version</version>
</dependency>
Documentation
Full documentation including examples, installation instructions, and API reference:
https://alfonsoristorato.github.io/jpa-spec-kotlin-dsl/
The API reference (KDoc) is at: https://alfonsoristorato.github.io/jpa-spec-kotlin-dsl/api/
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Built on top of Spring Data JPA
- Inspired by the need for more idiomatic Kotlin syntax in
JPA Specification and PredicateSpecification.
Support