英文:
ORDER BY in Criteria API for a computed column name (by alias)
问题
以下是翻译好的部分:
拥有一种情况,其中我的 Java 代码对应于查询 -
SELECT CUSTOMER_ID,
CUSTOMER_NAME,
CASE
WHEN COUNT (DISTINCT CARD_ID) > 1 THEN 'MULTIPLE'
ELSE MAX(CARD_NUM)
END AS CARD_NUM
FROM CUSTOMER LEFT JOIN CARD ON CARD.CUSTOMER_ID = CUSTOMER.CUSTOMER_ID
GROUP BY CUSTOMER_ID, CUSTOMER_NAME
用于详细信息的 Java 代码 -
CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<Tuple> query = cb.createQuery(Tuple.class);
final Root<Customer> root = query.from(Customer.class);
Expression<Object> caseSelect = cb.selectCase()
.when(cb.greaterThan(cb.countDistinct(join.get(Card_.cardId)), 1L), "MULTIPLE")
.otherwise(cb.greatest(Card_.get(Card_.cardNum)));
caseSelect.alias("card_num");
selects.add(caseSelect);
query.multiselect(selects).distinct(true);
query.groupBy(exprs);
query.orderBy(cb.asc(caseSelect));
现在,如何在 Criteria API 中进行排序。
1. 如果我按 root.CARD_NUM 进行排序,该属性不在 Root 中。- 抛出异常。
2. 如果我只对 caseSelect 表达式进行排序,它会抛出错误,指出 CARD_ID 不在查询的选择部分中。
3. 我不能在选择表达式中使用 card_num / card_id,对于查询来说这是不正确的。
有没有办法只按别名排序?我看到 Order 是 Expression 类型,如何从字符串名称获取表达式。我猜在 Hibernate 中可以做到这一点。无论如何在 Criteria API 中使用 Hibernate 的 orderby 是否可能?猜一个愚蠢的问题。
非常感谢您的帮助。
英文:
Having a situation where my java code is symbolic to query -
SELECT CUSTOMER_ID,
CUSTOMER_NAME,
CASE
WHEN COUNT (DISTINCT CARD_ID) > 1 THEN 'MULTIPLE'
ELSE MAX(CARD_NUM)
END AS CARD_NUM
FROM CUSTOMER LEFT JOIN CARD ON CARD.CUSTOMER_ID = CUSTOMER.CUSTOMER_ID
GROUP BY CUSTOMER_ID, CUSTOMER_NAME
Java code for detailed info -
CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<Tuple> query = cb.createQuery(Tuple.class);
final Root<Customer> root = query.from(Customer.class);
Expression<Object> caseSelect = cb.selectCase()
.when(cb.greaterThan(cb.countDistinct(join.get(Card_.cardId)), 1L), "MULTIPLE")
.otherwise(cb.greatest(Card_.get(Card_.cardNum)));
caseSelect.alias("card_num");
selects.add(caseSelect);
query.multiselect(selects).distinct(true);
query.groupBy(exprs);
query.orderBy(cb.asc(caseSelect));
Now, how to do the order by in Criteria API.
- If I do orderby root.CARD_NUM, the attribute is not present in Root.- Throws exception.
- If I do order by the caseSelect expression itself it throws an error
stating CARD_ID is not in SELECTED part of query. - I cannot have card_num / card_id in select expression, that is not right for the query.
Any way to just order by Alias name? I see Order is Expression type, and how to get an expression from string name. I guess you can do this in Hibernate. Would it be possible to use hibernate orderby in criteria API anyways ? Guess a stupid q
Any help is appreciated.
答案1
得分: 4
我面对过类似的情况...
query.multiselect(root, computedColumn);
query.orderBy(new Order[]{filterDTO.getSortAsc() ? cb.asc(cb.literal(2)) : cb.desc(cb.literal(2))});
在我的情况下,computedColumn 是一个 Subquery
query.orderBy(cb.asc(cb.literal(1)));
英文:
I faced a similar situation...
query.multiselect(root, computedColumn);
query.orderBy(new Order[]{filterDTO.getSortAsc() ? cb.asc(cb.literal(2)) : cb.desc(cb.literal(2))});
I my case computedColumn is Subquery<String>...I did not manage to make it work by column alias but it seems to work by column index returned in the tupple so I guess in your code it should work by index 1
query.orderBy(cb.asc(cb.literal(1)));
答案2
得分: 2
利用 Criteria API,你需要按 caseSelect
表达式进行排序。我尝试过了,在 Hibernate 5.4 中可以正常工作。你使用的是哪个版本?
英文:
Using the Criteria API, you need to order by the caseSelect
expression. I gave it a try and it works fine with Hibernate 5.4. Which version do you use?
答案3
得分: 1
似乎在JPA Criteria API中无法实现这一点,您将不得不回退到使用JPQL/HQL。
英文:
It seems this is not possible with the JPA Criteria API and you will have to fallback to using JPQL/HQL instead.
答案4
得分: 0
这几乎是不可能的,而且感觉就像是 JPA 漏掉的一个案例。
虽然如果使用 Hibernate API,是有可能的。但是,我的解决方法是:
- 创建一个包含 case 表达式的视图。
- 将该视图与我的实体进行连接(你不能直接进行连接,但可以再执行一个查询从 View.class 中获取数据)。
- 在 WHERE 子句中添加视图和实体的 ID。
现在在 ORDER BY 子句中,你可以按照 View.column_name 指定列名。
英文:
This is fairly not possible and just feels like a case missed by JPA.
Though if using hibernate API it is possible. But, my workaround was -
- Created a view which would contain the case expression.
- Join the view with my entity (you cannot do a join, but one more
query.from(View.class)). - In the where add the ids of View and the entity.
Now in the order by you could mention the column name by View.column_name.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论