[英]How to use ST_CONTAINS in Spring boot?
我有一個可以正常工作的服務器,可提供隨機文章,並保存在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
是已知的。
有根據的猜測:
@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))")
經過研究,我發現應該將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, ')'))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.