Is there a way I can pull off the below query (order by followed by a like) using hibernate criteria api?
select * from table where first_name like 'bar%' order by first_name like 'bar%' desc
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can’t easily map this query to Criteria API because
Orderclass only supports property names / aliases and not expressions.You can, however, try one of the following:
If the underlying database supports the expression you’re trying to order by in
SELECTclause, you can define a Projection with some alias (depending on expression you may have to useProjections.sqlProjection()to do so) and then order by that alias.Otherwise, you can extend
Orderclass and overwrite itstoSqlString()method to generate SQL as you see fit. Be careful that you don’t make it non-portable across different databases.P.S. To address some of the above comments, I’m pretty sure that
ORDER BY expressionis ANSI SQL standard (can’t be 100% sure, though, seeing as how it’s not publicly available). It’s certainly a de-facto standard – it’s supported by latest MySQL / PostgreSQL / Oracle / SQL Server versions.