Giter Site home page Giter Site logo

apulbere / crop Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 17 KB

a lightweight, zero dependency, and type-safe wrapper around Java Persistence Criteria API, which simplifies building queries, particularly useful within REST SQL (RSQL) context

License: Apache License 2.0

Java 100.00%
jakarta jakarta-persistence java rsql

crop's Introduction

CriteriaOperator (CrOp)

CriteriaOperator is a lightweight, zero dependency, and type-safe wrapper around Java Persistence Criteria API, which simplifies building queries, particularly useful within REST SQL (RSQL) context.

Example of REST query that can be interpreted by the library:

http://localhost:64503/pets?birthdate.gte=2010-01-11&nickname.like=Ba

Which results in the following select:

select
    p1_0.id,
    p1_0.birthdate,
    p1_0.name,
    p1_0.pet_type_id 
from
    pet p1_0 
where
    p1_0.name like ? escape '' 
    and p1_0.birthdate>=?

There are a couple of easy steps to achieve the above:

  1. Add the dependency
<dependency>
    <groupId>com.apulbere</groupId>
    <artifactId>crop</artifactId>
    <version>0.1.0</version>
</dependency>
  1. Define a DTO that contains all fields necessary for filtering. For example, if you want to do filtering on String type then you choose StringCriteriaOperator from com.apulbere.crop.operator package.
@Getter
@Setter
public class PetSearchCriteria {
    private StringCriteriaOperator nickname;
    private LocalDateCriteriaOperator birthdate;
}
  1. Create an instance of the service that will parse the query. It requires EntityManager in the constructor.
@Bean
CriteriaOperatorService cropService(EntityManager entityManager) {
    return new CriteriaOperatorService(entityManager);
}
  1. Finally, invoke the service's create method with the root entity and filter object. The result is a builder that lets you match the entity's meta-model with each field from the DTO. When done, execute the query.
@GetMapping("/pets")
List<PetRecord> search(PetSearchCriteria petSearchCriteria) {
    return cropService.create(Pet.class, petSearchCriteria)
            .match(PetSearchCriteria::getNickname, Pet_.name)
            .match(PetSearchCriteria::getBirthdate, Pet_.birthdate)
            .getResultList()
            .stream()
            .map(petMapper::map)
            .toList();
}

For full example and to see all the capabilities of the library checkout pet-shop demo repository.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.