Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a complex order-by using Hibernate Criteria?

I'm using Hibernate criteria and would like to add an order-by clause that is functionally the same as this SQL:

order by abs(dateSubmitted - 125234234)

Where dateSubmitted is a long and the number subtracted from it will be user-supplied (as a date). I'm trying to order records by their 'distance' from a user supplied date.

I've tried

criteria.addOrder("abs(dateSubmitted - " + getDateInput() + ")");

but it doesn't work.

Is this possible? Or will I have to abandon criteria for HQL? I have successfully done this in HQL but would like to stick with criteria if at all possible for consistency's sake.

like image 600
DLaw Avatar asked May 27 '10 21:05

DLaw


1 Answers

Maybe create your own Order class, like this:

http://blog.tremend.ro/2008/06/10/how-to-order-by-a-custom-sql-formulaexpression-when-using-hibernate-criteria-api/

I'm going to use this :)

like image 167
Kango_V Avatar answered Nov 07 '22 19:11

Kango_V