我有一个完全工作的服务器提供随机文章,保存在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很出名。
发布于 2019-07-29 19:58:01
经过一些研究,我发现应该将ST_CONTAINS与true进行比较。就像这样
@Query("SELECT an "
"FROM Announce as an " +
"WHERE true = ST_CONTAINS(?1, Point(an.latLng.longitude, an.latLng.latitude))")它对我起作用了,尽管我没有任何结果。
此外,即使MySQL能够理解Point,它也不能很好地映射到JPA。所以我拼接成这样
GeomFromText(CONCAT('POINT(', an.latLng.longitude, ' ', an.latLng.latitude, ')'))
发布于 2019-07-28 04:00:08
有根据的猜测:
@Query("SELECT an.<col_name> " + -- here should be column name, not a table alias
"FROM Announce as an " +
"WHERE ST_CONTAINS(?1, Point(an.latLng.longitude, an.latLng.latitude))")https://stackoverflow.com/questions/57235617
复制相似问题