blaze-persistence
blaze-persistence copied to clipboard
Add findAll method supporting pagination and dynamic fetch paths to EntityViewAwareRepository
Consider adding the following method to EntityViewAwareRepository.
Page<E> findAll(Specification<E> specification, Pageable pageable, boolean includeTotalCount,
String... fetch);
Potential implementation:
@Override
public Page<E> findAll(Specification<E> specification, Pageable pageable, boolean includeTotalCount,
String... fetch) {
BlazeCriteriaQuery<E> query = BlazeCriteria.get(cbf, entityInformation.getJavaType());
BlazeRoot<E> root = query.from(entityInformation.getJavaType());
BlazeCriteriaBuilder criteriaBuilder = query.getCriteriaBuilder();
Predicate predicate = specification.toPredicate(root, query,
criteriaBuilder);
if (predicate != null) {
query.where(predicate);
}
Sort sort = pageable.getSort();
if (sort.isSorted()) {
query.orderBy(toOrders(sort, root, criteriaBuilder));
}
CriteriaBuilder<E> cb = query.createCriteriaBuilder(em);
List<E> entities;
LongSupplier totalCountSupplier;
if (pageable.isPaged()) {
PagedList<E> pagedEntities = cb.page((int) pageable.getOffset(), pageable.getPageSize())
.withCountQuery(includeTotalCount)
.getResultList();
totalCountSupplier = pagedEntities::getTotalSize;
entities = pagedEntities;
} else {
entities = cb.getResultList();
totalCountSupplier = entities::size;
}
return PageableExecutionUtils.getPage(entities, pageable, totalCountSupplier);
}
This would be useful when one wants to do pagination on entities with dynamic fetches.
This would require fixing the following bugs first:
- https://github.com/Blazebit/blaze-persistence/issues/1770
- https://github.com/Blazebit/blaze-persistence/issues/2095