如果不为空,我需要按一个字段对查询结果进行排序,否则按另一个字段排序。
这是我的代码:
Expression condizione=cb.isNotNull(root.get(Attribuzione_.dataprincipale));
Expression principale=root.get(Attribuzione_.dataprincipale);
Expression creato=root.get(Attribuzione_.recordcreato);
Order ord=cb.desc(cb.selectCase()
.when(condizione, principale)
.otherwise(creato));
cq=cq.orderBy(ord);
此代码会导致内部异常:org.postgresql.util.PSQLException
因为 SQL 代码中的翻译不是我需要的“order by”:
ORDER BY CASE WHEN (t0.dataprincipale IS NOT NULL) THEN (t0.dataprincipale IS NOT NULL) ELSE t0.rec_creato END DESC
而不是
ORDER BY CASE WHEN (t0.dataprincipale IS NOT NULL) THEN t0.dataprincipale ELSE t0.rec_creato END DESC
但是我不明白我的代码哪里出了问题。
有什么建议吗? 谢谢
最佳答案
如果您使用的是 EclipseLink 2.3.x 或更早版本,请尝试升级到最新版本。
我已经用 2.3.x 测试了您的代码,但失败了,但从 2.4.x 接缝可以正常工作。
关于Java 持久性标准 API - orderBy() 中的 case 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37325250/