Spring JPA 查询无法识别空间类型

Gab*_*uza 1 java geometry jpa spring-boot sql-server-2017

我正在尝试使用 Spacial 数据类型通过 Spring JPA 查询注释发出本机查询请求。当要求通过控制台甚至在数据库中执行时,该查询可以完美运行。但是当他被要求通过Spring使用时。有谁知道我做错了什么?或者有更好的方法来实现相同的结果(将所有计算留在数据库侧)?先感谢您

这是我试图执行的查询。同样,它可以通过控制台工作,但无法通过 spring boot 请求执行

@Query(value = "SELECT TOP 1 * FROM Vehicles v " +
            "JOIN Bikelots l ON l.BikeLotId = v.BikeLotId " +
            "JOIN BikeTypes b ON b.BikeTypeId = l.BikeTypeId " +
            "WHERE b.BikeTypeId = ?1 " +
            "ORDER BY " +
            "Geography::STGeomFromText(v.Point.MakeValid().STAsText(),4326) " +
            ".STDistance(Geography::STGeomFromText(Geometry::Point(?2,?3,4326).MakeValid().STAsText(),4326)) "
            , nativeQuery = true)
Run Code Online (Sandbox Code Playgroud)
com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near 'Geography:'.
Run Code Online (Sandbox Code Playgroud)

我在使用这种方言application.properties

spring.jpa.database-platform=org.hibernate.spatial.dialect.sqlserver.SqlServer2008SpatialDialect
Run Code Online (Sandbox Code Playgroud)

Gab*_*uza 5

造成给定错误的原因是 jpa hibernate 将字符“:”识别为即将到来的变量的占位符。

通过将查询放入字符串变量中,然后在每个“:”之前添加“\\”并将该字符串分配给@Query的值,解决了问题。请参阅代码示例

String query = "SELECT TOP 1 * FROM Vehicles v " +
        "JOIN Bikelots l ON l.BikeLotId = v.BikeLotId " +
        "JOIN BikeTypes b ON b.BikeTypeId = l.BikeTypeId " +
        "WHERE b.BikeTypeId = ?1 " +
        "ORDER BY " +
        "Geography\\:\\:STGeomFromText(v.Point.MakeValid().STAsText(),4326) " +
        ".STDistance(Geography\\:\\:STGeomFromText(Geometry\\:\\:Point(?2,?3,4326).MakeValid().STAsText(),4326)) ";
    @Query(value = query, nativeQuery = true)

Run Code Online (Sandbox Code Playgroud)