Spring Data / Spring JPA:当查询有单引号时如何处理命名参数?

时间:2015-10-17 17:14:14

标签: java spring spring-data spring-data-jpa

我正在编写一个Spring Data Repository来查询PostGIS空间数据库。这是我在Entity类“MarketArea”中的命名本机查询:

@NamedNativeQuery(name = "MarketArea.findByLatLng",
   query = "SELECT p FROM MarketArea p WHERE " +
           " ST_Within(ST_GeomFromText('POINT(:longitude :latitude)', 4326), the_geom))",
   resultClass = MarketArea.class)

这是我在存储库中的查询方法:

@Query(nativeQuery = true)
Optional<MarketArea> findByLatLng(@Param("latitude") Double latitude, @Param("longitude") Double longitude);

当我运行时,我收到此错误:

org.springframework.dao.InvalidDataAccessApiUsageException: Parameter with that position [1] did not exist; nested exception is java.lang.IllegalArgumentException: Parameter with that position [1] did not exist

我认为它可能与查询中嵌入的单引号有关,因为当我删除引号时,似乎查询确实更接近数据库:

org.springframework.dao.InvalidDataAccessResourceUsageException: could
not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extractResultSet 
... 
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "$2"

我是否需要做一些事情来逃避单引号?

0 个答案:

没有答案