Kulwant
Kulwant

Reputation: 681

Hibernate MySQL Haversine

I am attempting to use the Haversine formula. Ideally, I would like to use Hibernate provider with JPA and MySQL. This is the following query that I am using which appears to work in MySQL.

SELECT campusid, ( 3959 * acos( cos( radians(37) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(-122) ) + sin( radians(37) ) * sin( radians( latitude ) ) ) ) AS distance FROM mydb.campus HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;

I am trying to see how I could use Hibernate and would like a cleaner way of doing this. I looked at the Criteria API but can't seem to find a way to execute this.

Upvotes: 0

Views: 655

Answers (1)

James DW
James DW

Reputation: 1815

That query is unlikely to be possible in Hibernate and certainly not in a clean way. You can however execute a native SQL query.

session.createSQLQuery("your sql here");

This will tie you to MySQL but should work.

Upvotes: 1

Related Questions