4

I'm writing a Spring Data Repository to query a PostGIS spatial database. Here's my named native query in the Entity class "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)

And here's my query method in the Repository:

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

When I run this, I get this error:

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

I think it might be related to the single quotes embedded in the query, because when I remove the quotes, it seems that the query does get closer to the database:

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"

Do I need to do something to escape single quotes?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
lreeder
  • 12,047
  • 2
  • 56
  • 65

0 Answers0