使用休眠空间条件查找圆圈内的点 - distanceWithin

时间:2018-01-19 14:33:07

标签: mysql hibernate spatial spatial-query hibernate-spatial

我有一个域对象car。其中一个汽车属性location存储在MySQL中,因为空间类型PointcolumnDefinition不能是Geometry,会引发异常)。

@Type(type="org.hibernate.spatial.GeometryType")
@Column(name = "location", columnDefinition="Point")
private Point location;

使用休眠空间标准我想得到那些半径范围内的标准。在本机sql中我可以使用ST_Distance_Sphere,但我想使用标准。问题是,这给了我一个错误org.hibernate.HibernateException: dwithin function not supported by this dialect

final Point circleCenterPoint = 
new GeometryFactory().createPoint(new Coordinate(latitude,longitude));

(...).add(SpatialRestrictions.distanceWithin(Car.LOCATION, circleCenterPoint, radiusInKm * 0.009));

我正在使用:jts-1.13hibernate-core-4.3.4hibernate-spatial-4.3(根据that,它们应匹配)以及org.hibernate.spatial.dialect.mysql.MySQLSpatial56Dialect

2 个答案:

答案 0 :(得分:2)

问题很简单,Hibernate Spatial版本4.x(或版本5.x)不支持函数ST_Distance_Sphere。

我创建了一张票证(https://hibernate.atlassian.net/browse/HHH-12229)来更新MySQL Spatial方言并添加对这些新功能的支持。但是,这些更改不会向后移植到版本4.x.

答案 1 :(得分:0)

作为“此方言不支持 dwithin 函数”的解决方法,我添加了 dwithin 函数,如下所示。

CREATE FUNCTION `dwithin`(`pt1` POINT, `pt2` POINT, `radius` FLOAT) RETURNS tinyint(1)
 DETERMINISTIC
BEGIN
 DECLARE dist FLOAT;
 SELECT ST_Distance_Sphere(pt1, pt2) INTO dist;
 RETURN dist<radius;
END
相关问题