gpt4 book ai didi

java - 如何使用 typedQuery 在 Criteria api 上添加分页?

转载 作者:行者123 更新时间:2023-12-02 05:48:02 26 4
gpt4 key购买 nike

我正在使用具有可分页功能的 Criteria API 来返回 Page<MyClass>可分页,但是当我插入 setFirstResult 时和setMaxResults查询始终返回 10 个元素。

如果我删除 setFirstResultsetMaxResults来自 TypedQuery,我的 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);
}

如果我搜索名为 ma​​rcos 的人,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);

关于java - 如何使用 typedQuery 在 Criteria api 上添加分页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56077215/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com