spring-data-specification-builder

Spring boot library that provides fluent API to define and compose specifications for data querying

License

License

Categories

Categories

Data
GroupId

GroupId

com.kirekov
ArtifactId

ArtifactId

spring-data-specification-builder
Last Version

Last Version

0.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

spring-data-specification-builder
Spring boot library that provides fluent API to define and compose specifications for data querying
Project URL

Project URL

https://github.com/SimonHarmonicMinor/spring-data-specification-builder
Source Code Management

Source Code Management

https://github.com/SimonHarmonicMinor/spring-data-specification-builder

Download spring-data-specification-builder

How to add to project

<!-- https://jarcasting.com/artifacts/com.kirekov/spring-data-specification-builder/ -->
<dependency>
    <groupId>com.kirekov</groupId>
    <artifactId>spring-data-specification-builder</artifactId>
    <version>0.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.kirekov/spring-data-specification-builder/
implementation 'com.kirekov:spring-data-specification-builder:0.2.0'
// https://jarcasting.com/artifacts/com.kirekov/spring-data-specification-builder/
implementation ("com.kirekov:spring-data-specification-builder:0.2.0")
'com.kirekov:spring-data-specification-builder:jar:0.2.0'
<dependency org="com.kirekov" name="spring-data-specification-builder" rev="0.2.0">
  <artifact name="spring-data-specification-builder" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.kirekov', module='spring-data-specification-builder', version='0.2.0')
)
libraryDependencies += "com.kirekov" % "spring-data-specification-builder" % "0.2.0"
[com.kirekov/spring-data-specification-builder "0.2.0"]

Dependencies

runtime (2)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-data-jpa jar
jakarta.persistence : jakarta.persistence-api jar 2.2.3

test (2)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-test jar
com.h2database : h2 jar

Project Modules

There are no modules declared in this project.

Spring Data Specification Builder

Spring boot library that provides fluent API to define and compose specifications for data querying.

Table of contents

Quick start

Maven:

<dependency>
    <groupId>com.kirekov</groupId>
    <artifactId>spring-data-specification-builder</artifactId>
</dependency>

Gradle:

dependencies {
    implementation 'com.kirekov:spring-data-specification-builder'
}

Status

Maven Central Build Status Quality Gate Status Coverage Code Smells Bugs

Usage

back to table of contents

Basic

Specification<Student> spec = FluentSpecificationBuilder
                     .combinedWithAnd()
                     .like("name", "%imo%")
                     .eq("age", 18)
                     .build();
List<Student> students = studentRepository.findAll(spec);

It is recommended to use hibernate-jpamodelgen in order to auto generate field names. The query would like this.

Specification<Student> spec = FluentSpecificationBuilder
                     .combinedWithAnd()
                     .like(Student_.NAME, "%imo%")
                     .eq(Student_.AGE, 18)
                     .build();
List<Student> students = studentRepository.findAll(spec);

By the way, the library supports type-safe links Attribute<Entity, ?>. So, the query can be enhanced with type checking.

Specification<Student> spec = FluentSpecificationBuilder
                     .combinedWithAnd()
                     .like(Student_.name, "%imo%")      // compiles only with Attribute<Student, ?>
                     .eq(Student_.age, 18)              // same
                     .build();
List<Student> students = studentRepository.findAll(spec);

Advanced

back to usage

Architecture

Interfaces diagram

FluentSpecificationBuilder is the entry point to the library and the only public implementation.

Complex queries

back to advanced

The defined conditions can be applied with either && or || logical expressions.

final var builderAnd = FluentSpecificaitonBuilder.<Student>combinedWithAnd();
final var builderOr = FluentSpecificaitonBuilder.<Student>combinedWithOr();

More than that, you can invert any condition with not(). Note that not() strictly requires condition. You can't build the specification with unclosed not().

final var spec = FluentSpecificationBuilder
                     .<Student>combinedWithAnd()
                     .like(Student_.name, "%a%")
                     .not().eq(Student_.age, 22)
                     .build();

If you have to provide complex condition that cannot be interpreted with the library, you can use specification() method.

final var spec = FLuentSpecificationBuilder()
                     .<Student>combinedWithAnd()
                     .eq(Student_.age, 20)
                     .specification((root, query, criteriaBuilder) -> /* your custom specification */)
                     .build();

That is also means that you can combine the results of different builders.

final var spec = FluentSpecificationBuilder()
                     .<Student>combinedWithOr()
                     .specification(
                         FluentSpecificationBuilder()
                             .<Student>combinedWithOr()
                             /* conditions */
                             .build()
                     )
                     .specification(
                         FluentSpecificationBuilder()
                             .<Student>combinedWithAnd()
                             /* conditions */
                             .build()
                     )
                     .build();

If you need to specify a field in a child entity, you can use PathFunction.

final var spec = FluentSpecificationBuilder
                     .<Student>combinedWithAnd()
                     .like(Student_.name, "%a%")
                     .not().eq(root -> root.get(Student_.university).get(University_.name), "MIT")
                     .buildDistinct();

Versions

Version
0.2.0
0.1.0
0.0.1