无法找到命名参数[longitude],在使用Spring JPA和QueryDSL时应为[searchTerm]之一

r7xajy2e  于 12个月前  发布在  Spring
关注(0)|答案(1)|浏览(138)

我的JPA查询似乎返回了正确的结果,但仍然抛出:

java.lang.IllegalArgumentException: Could not locate named parameter [longitude], expecting one of [searchTerm]
    at org.hibernate.query.internal.ParameterMetadataImpl.getNamedParameterDescriptor(ParameterMetadataImpl.java:229) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
    at org.hibernate.query.internal.ParameterMetadataImpl.getQueryParameter(ParameterMetadataImpl.java:198) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
    at org.hibernate.query.internal.QueryParameterBindingsImpl.getBinding(QueryParameterBindingsImpl.java:188) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
    at org.hibernate.query.internal.AbstractProducedQuery.setParameter(AbstractProducedQuery.java:490) ~[hibernate-core-5

字符串
我的JpaRepository有这个方法:

String HAVERSINE_FORMULA_IMPROVED = "(0.000621371 * 6378137 * 2 * atan2("
      + "      sqrt(sin(radians(c.geonamesPostal.latitude - :latitude) / 2) * sin(radians(c.geonamesPostal.latitude - :latitude) / 2)"
      + "          + cos(radians(:latitude)) * cos(radians(c.geonamesPostal.latitude))"
      + "              * sin(radians(c.geonamesPostal.longitude - :longitude) / 2) * sin(radians(c.geonamesPostal.longitude - :longitude) / 2)),"
      + "      sqrt(1 - sin(radians(c.geonamesPostal.latitude - :latitude) / 2) * sin(radians(c.geonamesPostal.latitude - :latitude) / 2)"
      + "          + cos(radians(:latitude)) * cos(radians(c.geonamesPostal.latitude))"
      + "              * sin(radians(c.geonamesPostal.longitude - :longitude) / 2) * sin(radians(c.geonamesPostal.longitude - :longitude) / 2))))";

  @Query("SELECT c FROM Classified c where c.dateDeleted is NULL and c.title like %:searchTerm% or c.description like %:searchTerm% ORDER BY "
      + HAVERSINE_FORMULA_IMPROVED)
  Page<Classified> findNearestClassifieds(@Param("latitude") double latitude,
      @Param("longitude") double longitude, @Param("searchTerm") String searchTerm, Pageable page);


我的猜测是我构造@Query的方式有问题

nwwlzxa7

nwwlzxa71#

你找到解决方案了吗?我已经通过Haversine函数在查询中工作了,但是现在,在更新到Spring 3.0(以及相应的hibernate libs等)之后,我得到了同样的错误(无法定位命名参数[longitude])。
如果我改变函数从:

String HAVERSINE_PART = " (6371 * " +
        " acos( cos(radians(:latitude)) " +
        "     * cos(radians(ii.latitude)) " +
        "     * cos(radians(ii.longitude) - radians(:longitude)) " +
        "     + sin(radians(:latitude)) " +
        "     * sin(radians(ii.latitude)) ) ) ";

字符串

String HAVERSINE_PART = "  (6371 * :longitude + :latitude )" ;


我没有得到错误。JPA可以处理的SQL的复杂性似乎是有限的。
HAVERSINE_PART既作为一个选择列包含,也包含在SQL语句的WHERE子句中。

相关问题