JPA Specification Kotlin DSL¶
A Kotlin DSL for building type-safe JPA queries using idiomatic Kotlin syntax.
Before and after¶
This is what a simple specification looks like with plain Spring Data JPA:
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:
Features¶
- Type-safe queries using Kotlin property references (
::) - no stringly-typed field names - Three DSL flavors covering
Predicate,Specification<T>, andPredicateSpecification<T> - Idiomatic Kotlin syntax with infix operators and the
/operator for nested properties - Composable with
andandoroperators - Zero overhead - pure extension functions on top of JPA, no proxies or runtime code generation
Modules¶
| Artifact | Purpose |
|---|---|
jpa-spec-kotlin-dsl |
Core DSL - works with any JPA provider |
jpa-spec-kotlin-dsl-hibernate |
Hibernate-specific extensions, requires the core module |
See Installation to get started, or jump straight to Quick Start.