看到 这个资源
的“空间查询”教程,其中包含特殊的方言和JTS库(开放源代码)。
基本上,您可以执行以下操作(从引用的页面复制/粘贴):
import com.vividsolutions.jts.geom.Geometry;import com.vividsolutions.jts.geom.Point;import com.vividsolutions.jts.io.ParseException;import com.vividsolutions.jts.io.WKTReader;import util.JPAUtil;import javax.persistence.EntityManager;import javax.persistence.Query;import java.util.Date;import java.util.List;
.......
private List find(String wktFilter) { Geometry filter = wktToGeometry(wktFilter); EntityManager em = JPAUtil.createEntityManager(); em.getTransaction().begin(); Query query = em.createQuery("select e from Event e where within(e.location, :filter) = true", Event.class); query.setParameter("filter", filter); return query.getResultList();}private Geometry wktToGeometry(String wktPoint) { WKTReader fromText = new WKTReader(); Geometry geom = null; try { geom = fromText.read(wktPoint); } catch (ParseException e) { throw new RuntimeException("Not a WKT string:" + wktPoint); } return geom;}有关生成圆的信息,请参见此资源(搜索“弧,圆和曲线”)。再次从那里复制/粘贴:
//this method replaces the above wktToGeometry() methodprivate static Geometry createCircle(double x, double y, final double RADIUS) { GeometricShapeFactory shapeFactory = new GeometricShapeFactory(); shapeFactory.setNumPoints(32); shapeFactory.setCentre(new Coordinate(x, y));//there are your coordinates shapeFactory.setSize(RADIUS * 2);//this is how you set the radius return shapeFactory.createCircle();}此外,您始终拥有一种解决方法,其中可以添加一些其他字段(使用映射
insertable=false,updatable=false)以映射到所使用的相同列
org.hibernate.spatial.GeometryType,然后在查询中使用它们。要计算距离,请检查欧几里德距离公式。



