Hi I have been trying to work out the best way to do this today to no avail.
What I would ideally like to do is to create an alias distance calculated by the SQL formula below (although I am open to other ways of calculating the distance, this was just the way that seemed it should be easiest)
Once I have that alias I want to be able to use it in a Restrictions fashion to find all that are within a certain distance.
I would also like to be able to sort via distance.
This is part of a bigger search criteria that is built up so I would ideally like to keep using Criteria. ( I already limit the range of Lat and Long values to make the distance calculation required on less fields.
Code:
Criteria criteria = session.createCriteria(Activity.class)
.createAlias("activityLocations", "actloc")
.createAlias("actloc.location", "location")
.createAlias("location.address", "addr");
criteria.add((Restrictions.and(Restrictions.between("addr.latitude", latmin,
latmax),Restrictions.between("addr.longitude", truelongmin, truelongmax))));
String sql = "SQRT( POW( 69.1 * ( addr3_.latitude - " + point[1]
+" ) , 2 ) + POW( 69.1 * ( "+point[0] +" - addr3_.longitude ) * COS( addr3_.latitude /"
+" 57.3 ) , 2 ) ) < "+distance;
criteria.add(Restrictions.sqlRestriction(sql));
Currently I have addr3_ in the sql query because I was looking at the verbose output and that is the way that Hibernate has generated the query (this hack worked in the one instance i was looking at but I dread to think about the longer term implications so would not want it to stay there!!)