views:

29

answers:

1

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?

+1  A: 

What is the best way in Hibernate to combine those two beasts?

To my knowledge, that's not possible. So either write everything using a Criteria query (I personally don't know how to do what you're asking for) or compute a dynamic HQL string.

Pascal Thivent
I hoped I could avoid dynamic string building. But I guess there's no other option. It would be great if hibernate supported this in the future!
apropoz