Home > Blockchain >  The custom query does not use the parameter
The custom query does not use the parameter

Time:09-17

It's been a few hours trying to figure out why my custom request isn't working.

I think I understood: the parameter does not apply on the request -> Jpa makes a request with 's_polygoneDepartement' instead of replacing it with the value, but I do not understand why.

@Query(value =  " select *  from poi where ST_Intersects(geog , \n"  
            "                     ST_GeomFromGeoJSON('\n"  
            " :s_polygoneDepartement \n"  
            "                    ')\n"  
            "                    ) = 'true' "
            ,
            countQuery = " select  count(*)  from poi where ST_Intersects(geog , \n"  
                    "                     ST_GeomFromGeoJSON('\n"  
                    " :s_polygoneDepartement \n"  
                    "                    ')\n"  
                    "                    ) = 'true' ",
            nativeQuery = true)
    Page<Poi> findAllByNomDepartementFr(@Param("s_polygoneDepartement") String s_polygoneDepartement, Pageable pageable);

I think it is because: s_polygoneDepartement is not close to a "=", but in this case how to make it understand that it should replace: s_polygoneDepartement by the value received?

thank you

CodePudding user response:

For me it seems that yoour line feeds (\n) are not needed. Because you wrap your parameter into '\n {} \n' Spring may treat it like String and not something that it can replace with given argument.

Try with:

@Query(value = "select * from poi where ST_Intersects(geog, ST_GeomFromGeoJSON(:s_polygoneDepartement)) = 'true' ",
       countQuery = "select count(*) from poi where ST_Intersects(geog, ST_GeomFromGeoJSON(:s_polygoneDepartement)) = 'true' ",
       nativeQuery = true)
Page<Poi> findAllByNomDepartementFr(@Param("s_polygoneDepartement") String s_polygoneDepartement, Pageable pageable);

Alternatively you may try with indexed query parameter instead of named parameter. Then you can discard @Parameter() and use ?1 in query instead of :s_polygoneDepartement.

  • Related