spring-data-jpa-criteria

Spring data JPA with criteria

License

License

Categories

Categories

Data
GroupId

GroupId

com.github.ydespreaux.spring.data
ArtifactId

ArtifactId

spring-data-jpa-criteria
Last Version

Last Version

1.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

spring-data-jpa-criteria
Spring data JPA with criteria
Project URL

Project URL

https://github.com/ydespreaux/spring-data-jpa-criteria
Source Code Management

Source Code Management

https://github.com/ydespreaux/spring-data-jpa-criteria

Download spring-data-jpa-criteria

How to add to project

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

Dependencies

compile (3)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter jar 2.1.0.RELEASE
org.projectlombok : lombok jar 1.18.2
org.springframework.boot : spring-boot-starter-data-jpa jar 2.1.0.RELEASE

test (5)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-test jar 2.1.0.RELEASE
org.springframework.boot : spring-boot-starter-validation jar 2.1.0.RELEASE
mysql : mysql-connector-java jar 8.0.13
com.github.ydespreaux.testcontainers : testcontainers-mysql jar 1.0.0
commons-lang : commons-lang jar 2.6

Project Modules

There are no modules declared in this project.

Spring Data Jpa Criteria

Spring Data JPA with criteria implementation

Introduction

This library defines a repository for performing dynamic queries using a criteria API.

Versions

spring-data-jpa-criteria spring-boot
1.2.0 2.1.x.RELEASE
1.1.0 2.0.x.RELEASE
1.0.1 1.5.x.RELEASE

Add maven dependency

<dependency>
    <groupId>com.github.ydespreaux.spring.data</groupId>
    <artifactId>spring-data-jpa-criteria</artifactId>
    <version>1.2.0</version>
</dependency>

API Criteria

Constructors
public Criteria();
public Criteria(String fieldName);
public Criteria(Field field);
Conjunctions
public Criteria and(Field field);
public Criteria and(String fieldName);
public Criteria and(Criteria criteria);
public Criteria and(Criteria... criterias);
public Criteria and(List<Criteria> criterias);
public Criteria or(Field field);
public Criteria or(Criteria criteria);
Operators
public Criteria eq(Object value);
public Criteria notEq(Object value);
public Criteria isNull();
public Criteria isNotNull();
public Criteria contains(String search);
public Criteria startsWith(String search);
public Criteria endsWith(String search);
public Criteria between(Comparable<?> lowerBound, Comparable<?> upperBound);
public Criteria lessThanEqual(Comparable<?> upperBound);
public Criteria lessThan(Comparable<?> upperBound);
public Criteria greaterThanEqual(Comparable<?> lowerBound);
public Criteria greaterThan(Comparable<?> lowerBound);
public Criteria in(Object... values);
public Criteria in(Iterable<?> values);
public Criteria notIn(Object... values);
public Criteria notIn(Iterable<?> values);

Samples

Condition : field <> 'value'
Criteria criteria = new Criteria("field").notEq("value");
Condition : field == 'value'
Criteria criteria = new Criteria("field").eq("value");
Condition : value1 <= field <= value2
Criteria criteria = new Criteria("field").between("value1", "value2");
Condition : field like '%value%'
Criteria criteria = new Criteria("field").contains("value");
Condition : field like '%value'
Criteria criteria = new Criteria("field").endsWith("value");
Condition : field like 'value%'
Criteria criteria = new Criteria("field").startsWith("value");
Condition : field > 10
Criteria criteria = new Criteria("field").greaterThan(10);
Condition : field >= 10
Criteria criteria = new Criteria("field").greaterThanEqual(10);
Condition : field < 10
Criteria criteria = new Criteria("field").lessThan(10);
Condition : field <= 10
Criteria criteria = new Criteria("field").lessThanEqual(10);
Condition : field in ('value1', 'value2')
Criteria criteria = new Criteria("field").in("value1", "value2");
Condition : field not in ('value1', 'value2')
Criteria criteria = new Criteria("field").notIn("value1", "value2");
Condition : field_1 = 'values_1' AND field_2 != 'values_2' AND field_3 like '%value_3%'
Criteria criteria = new Criteria("field_1").eq("value_1")
                .and("field_2").notEq("value_2")
                .and("field_3").contains("value_3");
Condition : field_1 = 'values_1' OR field_2 != 'values_2' OR field_3 like '%value_3%'
Criteria criteria = new Criteria("field_1").eq("value_1")
                .or("field_2").notEq("value_2")
                .or("field_3").contains("value_3");
Condition : field_1 = 'value_1' OR (field_2 != 'value_2' AND field_3 like '%value_3%')
Criteria criteria = new Criteria("field_1").eq("value_1")
                .or(new Criteria("field_2").notEq("value_2").and("field_3").contains("value_3"));
Condition : (field_1 = 'value_1' OR field_2 = 'value_2') AND field_3 = 'value_3'
Criteria criteria = new Criteria("field_1").eq("value_1")
                .or("field_2").eq("value_2")
                .and("field_3").eq("value_3");

JpaCriteriaRepository repository

The JpaCriteriaRepository interface implements search methods that take into account the criteria API.

public interface JpaCriteriaRepository<T, ID> extends JpaRepository<T, ID> {
    Optional<T> findOne(@Nullable Criteria criteria);
    Optional<T> findOne(@Nullable Criteria criteria, QueryOptions options);
    List<T> findAll(@Nullable Criteria criteria);
    List<T> findAll(@Nullable Criteria criteria, QueryOptions options);
    List<T> findAll(@Nullable Criteria criteria, Sort sort);
    List<T> findAll(@Nullable Criteria criteria, Sort sort, QueryOptions options);
    Page<T> findAll(@Nullable Criteria criteria, Pageable pageable);
    Page<T> findAll(@Nullable Criteria criteria, Pageable pageable, QueryOptions options);
    long count(@Nullable Criteria criteria);
    long count(@Nullable Criteria criteria, QueryOptions options);
}

Sample:

public interface AuthorRepository extends JpaCriteriaRepository<Author, Long> {
}

Spring Boot configuration

@Configuration
@EnableAutoConfiguration
@EntityScan(basePackages = {"com.github.ydespreaux.sample.jpa.criteria.domain"})
@EnableJpaCriteriaRepositories(basePackages = {"com.github.ydespreaux.sample.jpa.criteria.repository"})
public class JpaConfiguration {
}

Samples

Sample with Spring Boot and embedded database H2 :

https://github.com/ydespreaux/sample-project/tree/master/sample-spring-data-jpa-criteria

Versions

Version
1.2.0
1.1.0
1.0.1
1.0.0