views:

97

answers:

1

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.

A: 

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 :)

Kango_V
Nice. That's considerably less convoluted than I figured it would be, although a little hackish. I'll take it.
D Lawson