簡體   English   中英

如何使用 typedQuery 在 Criteria api 上添加分頁?

[英]How add pagination on Criteria api using typedQuery?

我正在使用帶有 pageable 的 Criteria API 來返回帶有setMaxResultsPage<MyClass> ,但是當我插入setFirstResultsetMaxResults ,查詢總是返回 10 個元素。

如果我從 TypedQuery 中刪除setFirstResultsetMaxResults ,我的typedQuery.getResultList()將返回所有元素,但顯然沒有分頁。

我有一個服務,它調用我的標准發送我的主要功能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;
}

我的存儲庫實現

@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);
    }

例如,如果我搜索名為marcos的人,則typedQuery.getResultList()僅返回 10 個元素,這些元素與頁數相同的元素(在我的數據庫中有 180 個)。 如果我刪除這個

typedQuery.setFirstResult(primeiroRegistro);
typedQuery.setMaxResults(totalRegistrosPorPagina);

然后typedQuery.getResultList()返回 180 個元素但有分頁,所有 180 個元素都在沒有分頁的單頁內

嘗試使用以下配置。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM