Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/apulbere/crop
a lightweight, zero dependency, and type-safe wrapper around Java Persistence Criteria API, which simplifies building queries, particularly useful within REST SQL (RSQL) context
https://github.com/apulbere/crop
jakarta jakarta-persistence java rsql
Last synced: 25 days ago
JSON representation
a lightweight, zero dependency, and type-safe wrapper around Java Persistence Criteria API, which simplifies building queries, particularly useful within REST SQL (RSQL) context
- Host: GitHub
- URL: https://github.com/apulbere/crop
- Owner: apulbere
- License: apache-2.0
- Created: 2024-01-21T09:34:51.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-01-30T08:28:54.000Z (11 months ago)
- Last Synced: 2024-01-30T09:38:19.683Z (11 months ago)
- Topics: jakarta, jakarta-persistence, java, rsql
- Language: Java
- Homepage:
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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:
```shell
http://localhost:64503/pets?birthdate.gte=2010-01-11&nickname.like=Ba
```Which results in the following select:
```sql
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
```xmlcom.apulbere
crop
0.1.0```
2. 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.
```java
@Getter
@Setter
public class PetSearchCriteria {
private StringCriteriaOperator nickname;
private LocalDateCriteriaOperator birthdate;
}
```
3. Create an instance of the service that will parse the query. It requires `EntityManager` in the constructor.
```java
@Bean
CriteriaOperatorService cropService(EntityManager entityManager) {
return new CriteriaOperatorService(entityManager);
}
```
4. 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.
```java
@GetMapping("/pets")
List 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](https://github.com/apulbere/pet-shop-crop) demo repository.