【发布时间】:2016-05-19 13:48:37
【问题描述】:
如果不为空,我需要按一个字段对我的查询结果进行排序,否则按另一个字段排序。
这是我的代码:
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);
此代码导致Internal Exception: org.postgresql.util.PSQLException
因为 SQL 代码中的翻译不是我需要的“排序依据”:
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
但我不明白我的代码哪里出了问题。
有什么建议吗? 谢谢
【问题讨论】:
标签: java jpa-2.0 criteria-api