Let's say I get the user to pick long and lat on map. I have database with fo开发者_如何学Gollowing columns:
zip
state
latitude
longitude
city
full_state
How do I retrieve the closest match for long and lat with hibernate HQL query?
One simple thing that you could do, is write your query so that you search for anything within a set range of from a given latitude and longitude. For example you could search for anything where:
latitude<(latitude+<your boundary>) and latitude>(latitude-<your boundary>) and longitude<(longitude+<your boundary>) and longitude>(longitude-<your boundary>)
I think 1 degree of latitude or longitude is about about 69.11 miles. so to find anything within 50 miles set your boundary to be around 0.8
This answer wont answer your question specifically, but there are special database engines to handle geographical data, because otherwise, as you mentioned, you need to go through all you data and calculate the distance for every user request.
I've done something like that before using oracle spatial... reading that wiki page, I found that there's an extension for Postgres called PostGis which also allows spatial queries.
I know that it's not hibernate, but I strongly suggest you to check those engines.
why don't use simply type Point or Geom in hibernate? with those types you can execute all query you want with hql and POSTGIS? you can create point simply like this :
ST_POINTFROMTEXT('POINT(LONGITUDE LATITUDE)')
????
精彩评论