[英]How add pagination on Criteria api using typedQuery?
I am using Criteria API with pageable to return a Page<MyClass>
with pageable, but when I insert setFirstResult
and setMaxResults
the query always returns 10 elements.我正在使用带有 pageable 的 Criteria API 来返回带有
setMaxResults
的Page<MyClass>
,但是当我插入setFirstResult
和setMaxResults
,查询总是返回 10 个元素。
If I remove setFirstResult
and setMaxResults
from TypedQuery, my typedQuery.getResultList()
returns all elements but obviously without pagination.如果我从 TypedQuery 中删除
setFirstResult
和setMaxResults
,我的typedQuery.getResultList()
将返回所有元素,但显然没有分页。
I have a service that calls my criteria sending my pageable for the main function peopleRepository.filter
我有一个服务,它调用我的标准发送我的主要功能
peopleRepository.filter
可分页
public Page<People> filtrarParcial(String name, String rg, String mom, String cpf, String nickname, Integer pageNumber, Integer pageSize, List<String> sort) {
List<Sort.Order> orders = new ArrayList<>();
PageRequest pageRequest = PageRequest.of(pageNumber, pageSize, Sort.by(orders));
Page<People> listPeople = peopleRepository.filter(name, rg, mom, cpf, nickname, pageRequest);
return listPeople;
}
My repository implements我的存储库实现
@Service
public class PeopleRepositoryImpl implements PeopleRepositoryQueries {
@PersistenceContext
private EntityManager manager;
@Transactional(readOnly = true)
public Page<People> filter(String name, String rg, String mom, String cpf, String nickname, Pageable pageable) {
CriteriaBuilder criteriaBuilder = manager.getCriteriaBuilder();
CriteriaQuery<People> query = criteriaBuilder.createQuery(People.class);
Root<People> root = query.from(People.class);
Path<String> nomePath = root.<String>get("name");
List<Predicate> predicates = new ArrayList<Predicate>();
if(!nome.equals("")) {
Predicate nomeIgual = criteriaBuilder.like(nomePath, "%" +name.toUpperCase() + "%");
predicates.add(nomeIgual);
}
query.where((Predicate[]) predicates.toArray(new Predicate[0]));
int paginaAtual = pageable.getPageNumber();
int totalRegistrosPorPagina = pageable.getPageSize();
int primeiroRegistro = paginaAtual * totalRegistrosPorPagina;
TypedQuery<People> typedQuery = manager.createQuery(query);
// typedQuery.setFirstResult(primeiroRegistro);
// typedQuery.setMaxResults(totalRegistrosPorPagina);
Integer totalRows = typedQuery.getResultList().size();
Long total = totalRows.longValue();
return new PageImpl<>(typedQuery.getResultList(), pageable, total);
}
If I search for example a people with name marcos , typedQuery.getResultList()
returns only 10 elements coincidentally with the same number elements by page (in my database there are 180).例如,如果我搜索名为marcos的人,则
typedQuery.getResultList()
仅返回 10 个元素,这些元素与页数相同的元素(在我的数据库中有 180 个)。 If I remove this如果我删除这个
typedQuery.setFirstResult(primeiroRegistro);
typedQuery.setMaxResults(totalRegistrosPorPagina);
then typedQuery.getResultList()
returns 180 elements but with pagination, and all 180 elements are within single page without pagination然后
typedQuery.getResultList()
返回 180 个元素但有分页,所有 180 个元素都在没有分页的单页内
try with the below configurations.尝试使用以下配置。
CriteriaQuery<Long> countQuery = criteriaBuilder.createQuery(Long.class);
Root<People> entity_ = countQuery.from(query.getResultType());
entity_.alias("entitySub"); //use the same alias in order to match the restrictions part and the selection part
countQuery.select(criteriaBuilder.count(entity_));
Predicate restriction = query.getRestriction();
if (restriction != null) {
countQuery.where(restriction); // Copy restrictions
}
Long totalCount=entityManager.createQuery(countQuery).getSingleResult();
query.setFirstResult(pageable.getOffset());
query.setMaxResults(pageable.getPageSize());
List<People> result = query.getResultList();
return PageableExecutionUtils.getPage(result,pageable, () -> totalCount);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.