You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
By clicking “Sign up for GitHub”, you agree to our
terms of service
and
privacy statement
. We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Currently, the JPA spec only allows column names or paths (embeddables only) in the
ORDER BY
clause in JPQL expressions, but also for the
@OrderBy
annotation or XML element (more or less).
From the JPA 2.2 spec PDF:
It's often worthwhile (or even required) to get an
expression
into the
ORDER BY
clause to do the sorting. It's also much more natural to allow custom (JPQL) expressions to match what SQL is able to accomplish.
Examples:
https://stackoverflow.com/questions/59587201/jpa-hibernate-case-in-orderby-annotation-throws-exception-unexpected-token-ca
(JPA annotation, not working)
https://discourse.hibernate.org/t/mapping-orderby-with-case-when-fails-with-npe-unexpected-token-case/3589
(JPA + Hibernate annotations, not working)
https://stackoverflow.com/questions/19300911/case-when-in-hibernate-orderby-annotation
(Hibernate annotation, not working)
https://stackoverflow.com/questions/9907871/order-by-condition-hql
(Hibernate HQL, not working)
https://stackoverflow.com/questions/30903801/hql-order-by-expression
(Hibernate HQL, not working)
The only way to achieve
ORDER BY
by some expression seems to be to add that expression to the
SELECT <my expression>
along with an
AS result_variable
, see JPQL BNF:
select_clause ::= SELECT [DISTINCT] select_item {, select_item}*
select_item ::= select_expression [[AS] result_variable]
select_expression ::=
single_valued_path_expression |
scalar_expression |
aggregate_expression |
identification_variable |
OBJECT(identification_variable) |
constructor_expression
orderby_clause ::= ORDER BY orderby_item {, orderby_item}*
orderby_item ::=
state_field_path_expression | general_identification_variable | result_variable
[ ASC | DESC ]
But what about the
@OrderBy
annotation?
Seems to be an issue for quite some people out there.
Please extend.