gpt4 book ai didi

java - Crieria Builder setMaxResults 排序问题

转载 作者:行者123 更新时间:2023-12-02 10:13:00 27 4
gpt4 key购买 nike

我在 Criteria Builder 中编写了一个查询,它看起来像这样。

private EntityManager entitymanager;
CriteriaBuilder criteriaBuilder = entitymanager.getCriteriaBuilder();
CriteriaQuery<Object> criteriaQuery =
criteriaBuilder.createQuery(Object.class);
Root<Employee> from = criteriaQuery.from(Employee.class);
criteriaQuery.orderBy(criteriaBuilder.asc(name));
TypedQuery<Object> typedQuery1 = entitymanager.createQuery(criteriaQuery );
List<Object> resultlist1 = typedQuery1.getResultList();
typedQuery1.setMaxResults(pageable.getPageSize());

setMaxResults应用于查询后,resultlist1的顺序为和以前不一样?为什么会出现这样的情况呢?我怎样才能阻止这种行为?

最佳答案

为什么使用 Object 作为类型?使用员工

CriteriaQuery<Employee> criteriaQuery = 
criteriaBuilder.createQuery(Employee.class);
Root<Employee> from = criteriaQuery.from(Employee.class);
criteriaQuery.orderBy(criteriaBuilder.asc(from.get("column_name")));
TypedQuery<Employee> typedQuery1 = entitymanager.createQuery(criteriaQuery);
typedQuery1.setMaxResults(pageable.getPageSize());
List<Employee> resultlist1 = typedQuery1.getResultList();

执行查询后调用 setMaxResults() 没有意义。在调用 getResultList() 之前设置最大结果

typedQuery1.setMaxResults(pageable.getPageSize());
List<Employee> resultlist1 = typedQuery1.getResultList();

什么是name变量?像这样订购:

criteriaQuery.orderBy(criteriaBuilder.asc(from.get("column_name")));

关于java - Crieria Builder setMaxResults 排序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54860802/

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