1

Is is possible using Hibernate (prefferably the Criteria API) to use a mySql function to Order By.

The function I am looking to use is :-

SQRT( POW( 69.1 * ( {alias}.latitude -  51.3814282 ) , 2 ) + POW( 69.1 * (   -2.3574537 - {alias}.longitude ) * COS( {alias}.latitude / 57.3 ) , 2 ) ) 

where the {alias} is rooted at

criteria.createCriteria("location.address"); 

All I am looking to do is have the results ordered by the above distance equation, but thus far I havent found a way using the Projections options or the Order. options and am running out of ideas.

Cheers, R

1 Answer 1

1

This doesn't exactly answer your question but what you get out of the Criteria API is a bunch of nice Objects which adhere to their interfaces. Have you thought of lifting the ordering out of the query and instead sorting them in memory? It should be easy to express that ordering function in terms of a Comparator and then you can just sort the Collection.

I don't know how to express it in the .addOrder function of the Criteria API. You can add arbitrary SQL to Restrictions, but it looks like you can't order by arbitrary SQL.

Good luck.

Sign up to request clarification or add additional context in comments.

1 Comment

Cheers Tim, it is not exactly what I was looking for but it does do the job. I am slightly concerned about performance as the dataset grows but for now it will do just fine to sort in memory :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.