I use Hibernate in a storefinder application. For the proximity search in SQL I use the haversine formula. Because this is a bit messy SQL I created a named SQL query in my .hbm.xml File for this.
SELECT
location.*,
( 3959 * acos( cos( radians(7.4481481) ) *
cos( radians( X(location.coordinates) ) ) *
cos( radians( Y(location.coordinates) ) - radians(46.9479986) ) +
sin( radians(7.4481481) ) * sin( radians( X(location.coordinates) ) ) ) )
AS distance
FROM
location
WHERE
location.coordinates IS NOT NULL
HAVING
distance < :radius
ORDER BY
distance ASC
LIMIT :max
But I also have a user defined filter (opening hours, assortments, etc.). For this I use Hibernate criteria to programatically add filters.
Now I have a perfectly working NamedQuery giving me all locations around a certain point and a perfectly working criteria query giving me all locations according to some filter.
My question is: What is the best way in Hibernate to combine those two beasts? (i.e. I need all locations around a certain point satisfying some filter.) Is there for example any way to use a NamedQuery as a subquery in a criteria search?