【问题标题】:How to use ST_CONTAINS in Spring boot?如何在 Spring Boot 中使用 ST_CONTAINS?
【发布时间】:2019-07-27 19:58:12
【问题描述】:

我有一个完全工作的服务器,用于提供随机文章,保存在 MySQL 数据库中,我希望能够在多边形内找到文章。

我发现MySQL已经支持PolygonPoint所以我只使用了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 是已知的。

【问题讨论】:

    标签: mysql spring-boot jpa


    【解决方案1】:

    经过一番研究,我发现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, ')'))

    【讨论】:

      【解决方案2】:

      有根据的猜测:

      @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))")
      

      【讨论】:

        猜你喜欢
        • 2021-06-19
        • 2020-11-15
        • 2015-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多