我有一个类
我需要按照
Lawsuit
,它包含一个List<Hearing>
,每个Hearing
都有一个Date
属性。我需要按照
Hearing
的日期顺序选择所有的Lawsuit
。
我有一个如下的CriteriaQuery:CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Lawsuit> cq = cb.createQuery(Lawsuit.class);
Root<Lawsuit> root = cq.from(Lawsuit.class);
我使用distinct来压缩结果:
cq.select(root).distinct(true);
我随后参加Lawsuit
与Hearing
Join<Lawsuit, Hearing> hearing = root.join("hearings", JoinType.INNER);
创建 Predicate
。predicateList.add(cb.isNotNull(hearing.<Date>get("date")));
以及 订单
:
orderList.add(cb.asc(hearing.<Date>get("date")));
如果我避免使用 distinct
,一切都正常,但是如果我使用它,就会出现以下错误:它抱怨无法根据未在 SELECT 中的字段排序:
Caused by: org.postgresql.util.PSQLException: ERROR: for
SELECT DISTINCT
,ORDER BY
expressions must appear in select list
List<Hearing>
已经可以通过返回的 Lawsuit
类访问,所以我感到困惑:我应该如何将它们添加到选择列表中?