Skip to content

Commit

Permalink
javadoc update, readme update
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksadacic committed Jan 31, 2025
1 parent 1b06537 commit db5efb4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ and `SearchOperator`.
- **Nested Filters**: Build recursive AND/OR conditions.
- **Multiple Joins**: Deep join. Filter by your joined attribute (e.g. user.card.bank.bankName).
- **Minimal Boilerplate**: No need to hand-write every `Specification` --the library does it for you.
- **Projections** simplified. No need to write `@Query` in your repository, just provide a POJO.
- **Projections** simplified. Just provide a POJO or an interface with getters in the `executeQuery` method for valid
mapping.
- **Integration with Spring Data**: Seamlessly use `Specification`, `Pageable`, etc.

## Compatibility
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,22 +165,22 @@ static <T> Query<T> where(String attribute, SearchOperator searchOperator, Objec
*
* @param entityManager The {@link EntityManager} to execute the query.
* @param entityClass The type of the entity being queried.
* @param pojo The class of the POJO to map the results to.
* @param returnType The class (or interface with getters) of the POJO to map the results to.
* @param <R> The type of the result (POJO).
* @return A list of results mapped to the specified DTO class.
*/
<R> List<R> executeQuery(EntityManager entityManager, Class<T> entityClass, Class<R> pojo);
<R> List<R> executeQuery(EntityManager entityManager, Class<T> entityClass, Class<R> returnType);

/**
* Executes the query with all the applied conditions using the given {@link EntityManager} and returns a {@link Page}.
*
* @param entityManager The {@link EntityManager} to execute the query.
* @param entityClass The type of the entity being queried.
* @param pojo The class of the POJO to map the results to.
* @param returnType The class (or interface with getters) of the POJO to map the results to.
* @param <R> The type of the result (POJO).
* @return A page of results mapped to the specified DTO class.
*/
<R> Page<R> executeQuery(EntityManager entityManager, Class<T> entityClass, Class<R> pojo, PageRequest pageRequest);
<R> Page<R> executeQuery(EntityManager entityManager, Class<T> entityClass, Class<R> returnType, PageRequest pageRequest);

/**
* Builds the {@link Specification} for the query, representing all the applied conditions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,24 +144,24 @@ public Specification<T> buildSpecification() {
}

@Override
public <R> List<R> executeQuery(EntityManager entityManager, Class<T> entityClass, Class<R> pojo) {
Map.Entry<CriteriaQuery<Tuple>, Root<T>> preparedQueryObjects = ExecuteQueryUtils.prepareCriteriaQuery(entityManager, entityClass, pojo, distinct, specification);
public <R> List<R> executeQuery(EntityManager entityManager, Class<T> entityClass, Class<R> returnType) {
Map.Entry<CriteriaQuery<Tuple>, Root<T>> preparedQueryObjects = ExecuteQueryUtils.prepareCriteriaQuery(entityManager, entityClass, returnType, distinct, specification);
CriteriaQuery<Tuple> criteriaQuery = preparedQueryObjects.getKey();

// Execute the query
TypedQuery<Tuple> query = entityManager.createQuery(criteriaQuery);
List<Tuple> results = query.getResultList();

List<Map<String, Object>> mappedResults = QueryUtils.mapTuplesToFieldValues(results, pojo);
List<Map<String, Object>> mappedResults = QueryUtils.mapTuplesToFieldValues(results, returnType);

// Map the results to DTOs using reflection
ObjectMapper mapper = new ObjectMapper();
return QueryUtils.convertToDtoList(pojo, mappedResults, mapper);
return QueryUtils.convertToDtoList(returnType, mappedResults, mapper);
}

@Override
public <R> Page<R> executeQuery(EntityManager entityManager, Class<T> entityClass, Class<R> pojo, PageRequest pageRequest) {
Map.Entry<CriteriaQuery<Tuple>, Root<T>> preparedQueryObjects = ExecuteQueryUtils.prepareCriteriaQuery(entityManager, entityClass, pojo, distinct, specification);
public <R> Page<R> executeQuery(EntityManager entityManager, Class<T> entityClass, Class<R> returnType, PageRequest pageRequest) {
Map.Entry<CriteriaQuery<Tuple>, Root<T>> preparedQueryObjects = ExecuteQueryUtils.prepareCriteriaQuery(entityManager, entityClass, returnType, distinct, specification);
CriteriaQuery<Tuple> criteriaQuery = preparedQueryObjects.getKey();
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
Root<T> root = preparedQueryObjects.getValue();
Expand Down Expand Up @@ -189,11 +189,11 @@ public <R> Page<R> executeQuery(EntityManager entityManager, Class<T> entityClas
Long totalElements = entityManager.createQuery(countQuery).getSingleResult();

// Map the tuples to a list of maps with field values
List<Map<String, Object>> mappedResults = QueryUtils.mapTuplesToFieldValues(results, pojo);
List<Map<String, Object>> mappedResults = QueryUtils.mapTuplesToFieldValues(results, returnType);

// Map the results to DTOs using reflection
ObjectMapper mapper = new ObjectMapper();
List<R> content = QueryUtils.convertToDtoList(pojo, mappedResults, mapper);
List<R> content = QueryUtils.convertToDtoList(returnType, mappedResults, mapper);

// Return a Page containing the content and pagination metadata
return new PageImpl<>(content, pageRequest, totalElements);
Expand Down

0 comments on commit db5efb4

Please sign in to comment.