我有一个功能齐全的服务器,提供随机文章,保存在 MySQL 数据库中,我想让在多边形内查找文章成为可能。
我发现 MySQL 已经支持Polygon
,Point
所以我只使用了ST_CONTAINS
,它也被支持。由于文章确实有嵌入的纬度和经度,我想我可以创建一个点并尝试找出它是否包含在多边形中。
import org.springframework.data.geo.Polygon;
// ...I pass the JPA declaration...
@Query("SELECT an " +
"FROM Announce as an " +
"WHERE ST_CONTAINS(?1, Point(an.latLng.longitude, an.latLng.latitude))")
List<Announce> findAllInPolygon(Polygon polygonPoints);
我没有看到错误,但在运行时,我收到一个致命错误:
Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException:
unexpected AST node: ( near line 1, column 80 [SELECT an FROM com.desoftmotion.findnow.domain.Announce
as an WHERE ST_CONTAINS(POLYGON(?1), Point(an.latLng.longitude, an.latLng.latitude))]
我不明白,因为我认为ST_CONTAINS
是已知的。
经过一番研究,我发现ST_CONTAINS
应该与true
. 就像是
@Query("SELECT an "
"FROM Announce as an " +
"WHERE true = ST_CONTAINS(?1, Point(an.latLng.longitude, an.latLng.latitude))")
它对我有用,尽管我没有任何结果。
而且,Point
即使它被 MySQL 理解,它也不能被 JPA 很好地映射。所以我连接了像
GeomFromText(CONCAT('POINT(', an.latLng.longitude, ' ', an.latLng.latitude, ')'))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句