Java JPA Criteria Query API 和按两列排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18389378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:08:36  来源:igfitidea点击:

JPA Criteria Query API and order by two columns

javajpasql-order-bycriteria-api

提问by kmansoor

I'm stuck with a simple problem; struggling how to invoke order byon a joined entity. Essentially I am trying to achieve the following with JPA Criteria:

我被一个简单的问题困住了;奋力如何调用order byjoin编辑实体。本质上,我试图通过以下方式实现以下目标JPA Criteria

select distinct d from Department d 
left join fetch d.children c 
left join fetch c.appointments a
where d.parent is null 
order by d.name, c.name

I have the following:

我有以下几点:

CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
CriteriaQuery<Department> c = cb.createQuery(Department.class);
Root<Department> root = c.from(Department.class);
Fetch<Department, Department> childrenFetch = root.fetch(
    Department_.children, JoinType.LEFT);
childrenFetch.fetch(Department_.appointments, JoinType.LEFT);

c.orderBy(cb.asc(root.get(Department_.name)));
c.distinct(true);
c.select(root);
c.where(cb.isNull(root.get(Department_.parent)));

How to achieve order by d.name, c.namewith Criteria API? I tried with Expression, Path but didn't work. Any pointers will be greatly appreciated.

如何实现order by d.name, c.nameCriteria API?我尝试使用 Expression、Path 但没有奏效。任何指针将不胜感激。

回答by Adamsan

I was having trouble doing the same, and I have found a solution on this page: http://www.objectdb.com/api/java/jpa/criteria/CriteriaQuery/orderBy_Order_

我在做同样的事情时遇到了麻烦,我在这个页面上找到了一个解决方案:http: //www.objectdb.com/api/java/jpa/criteria/CriteriaQuery/orderBy_Order_

//javax.persistence.criteria.CriteriaQuery
//CriteriaQuery<T> orderBy(Order... o)

Specify the ordering expressions that are used to order the query results. Replaces the previous ordering expressions, if any. If no ordering expressions are specified, the previous ordering, if any, is simply removed, and results will be returned in no particular order. The left-to-right sequence of the ordering expressions determines the precedence, whereby the leftmost has highest precedence.

Parameters: o - zero or more ordering expressions

Returns: the modified query

Since: JPA 2.0

指定用于对查询结果进行排序的排序表达式。替换之前的排序表达式(如果有)。如果未指定排序表达式,则简单地删除先前的排序(如果有),并且结果将不按特定顺序返回。排序表达式从左到右的顺序决定了优先级,最左边的优先级最高。

参数: o - 零个或多个排序表达式

返回:修改后的查询

从:JPA 2.0

回答by levo4ka

If you need to add couple of orders you can make something like (but for your query and different root objects)

如果您需要添加几个订单,您可以制作类似的东西(但对于您的查询和不同的根对象)

CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Route> query = criteriaBuilder.createQuery(Route.class);
Root<Route> routeRoot = query.from(Route.class);
query.select(routeRoot);

List<Order> orderList = new ArrayList();
query.where(routeRoot.get("owner").in(user));

orderList.add(criteriaBuilder.desc(routeRoot.get("date")));
orderList.add(criteriaBuilder.desc(routeRoot.get("rating")));

query.orderBy(orderList);

回答by Saif

I have the same problem with order by using Criteria API. I found this solution:

我在使用 Criteria API 时遇到了与订单相同的问题。我找到了这个解决方案:

CriteriaQuery<Test> q = cb.createQuery(Test.class);
Root<Test> c = q.from(Test.class);
q.select(c);
q.orderBy(cb.asc(c.get("name")), cb.desc(c.get("prenom")));

回答by pradeep

categoryRepository.findAll(predicates, new Sort(Direction.ASC, "sortOrder", "name")) .forEach(categoryDtoList::add);

categoryRepository.findAll(predicates, new Sort(Direction.ASC, "sortOrder", "name")) .forEach(categoryDtoList::add);

回答by Jorge Santos Neill

The solucion that work for me is the following

对我有用的解决方案如下

session=HibernateUtil.getSessionJavaConfigFactory_report().openSession();
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<Object[]> criteriaQuery = builder.createQuery(Object[].class);
List<Order> orderList = new ArrayList();
orderList.add(builder.desc(ejeRoot.get("name")));
criteriaQuery.orderBy(orderList);

Note: ejeRoot is the class object

注意:ejeRoot 是类对象