简体   繁体   English

Spring 数据 JPA 本机查询 - 未注册命名参数

[英]Spring Data JPA Native Query - Named parameters are not being registered

I have the following repository class backed by a postgres database with the postgis extension installed.我有以下存储库 class 由安装了 postgis 扩展的 postgres 数据库支持。

import java.util.List;
import java.util.Set;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface ForumRepository extends PagingAndSortingRepository<ForumEntity, Long> {


  @Query(value = "SELECT *"
      + "  FROM forums"
      + "  WHERE ST_DWithin(location, 'POINT(:lon :lat)', :meters,true)"
      + "  ORDER BY ST_Distance(location, 'POINT(:lon  :lat)') ASC",
      countQuery = "SELECT count(*) FROM forums WHERE ST_DWithin(location, 'POINT(:lon :lat)', :meters,true)",
      nativeQuery = true)
  List<ForumEntity> findInRadius(@Param("lat") Double lat,@Param("lon") Double lon,@Param("meters") Double meters, Pageable pageable);

}

This Spring Data JPA repository contains one method definition which queries the database to find all forums within a certain radius.此 Spring 数据 JPA 存储库包含一个方法定义,该方法定义查询数据库以查找特定半径内的所有论坛。 I am using named params with a native query combined with postgis's ST_DWithin method to perform this query.我将命名参数与本机查询结合使用 postgis 的ST_DWithin方法来执行此查询。

Spring Boot starts up fine without complaint and reports that the application has started successfully. Spring Boot 启动正常,没有抱怨,并报告应用程序已成功启动。 However when I try to call that query I get the following exception - Parameter with that name [lat] did not exist; nested exception is java.lang.IllegalArgumentException: Parameter with that name [lat] did not exist但是,当我尝试调用该查询时,出现以下异常 - Parameter with that name [lat] did not exist; nested exception is java.lang.IllegalArgumentException: Parameter with that name [lat] did not exist Parameter with that name [lat] did not exist; nested exception is java.lang.IllegalArgumentException: Parameter with that name [lat] did not exist

I have tried switching over to positioned parameters, but got the same error that the parameter does not exist.我尝试切换到定位参数,但得到参数不存在的相同错误。

I am using Spring Boot 1.5.4我正在使用 Spring 引导 1.5.4

Any ideas?有任何想法吗?

The single quotes around your parameters is probably the issue.参数周围的单引号可能是问题所在。 Have a look at this question .看看这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM