Hibernate version: 1.0.2.0
Name and version of the database you are using: Oracle
Currently, I have a query that works in SQL, but I would like to make it work with HQL. I have two related tables:
Store : Store_Id, Zipcode_Id
Zipcode: Zipcode_id, Zipcode, Lat, Long
i'm using an earth distance calculation to determine what stores are nearby based on a users entered zipcode.
Link to calculation:
http://www.meridianworlddata.com/Distance-Calculation.asp
In the following query, :zipLat and :zipLng are looked up against the zipcode table based upon the entered zipcode.
Code:
select
{ci.*}
from Store ci inner join Zipcode zc ON
ci.Zipcode_id = zc.Zipcode_id
WHERE sqrt( power( 69.1 * (zc.Lat - :zipLat), 2 ) +
power( 69.1 * (zc.Lng - :zipLng) * cos(:zipLat/57.3), 2) ) <= :maxDistance
order by sqrt( power( 69.1 * (zc.Lat - :zipLat), 2 ) +
power( 69.1 * (zc.Lng - :zipLng) * cos(:zipLat/57.3), 2) )
Distance is calculated, but I need to sort/order by it. I tried the following HQL, but I think I'm barking up the wrong tree. Order by cannot contain Arithmetic operators. Any suggestions? :
Code:
select ci
from Store as ci, Zipcode as zc
where ci.Zipcode = zc and
sqrt( power( 69.1 * (zc.Lat - :zipLat), 2 ) +
power( 69.1 * (zc.Lng - :zipLng) * cos(:zipLat/57.3), 2) ) <= :maxDistance
group by ci
order by max( sqrt( power( 69.1 * (zc.Lat - :zipLat), 2 ) +
power( 69.1 * (zc.Lng - :zipLng) * cos(:zipLat/57.3), 2) ) )
Thanks much!