【发布时间】:2018-04-05 20:11:11
【问题描述】:
我正在尝试实现以下查询,以便在给定位置 (lat,lng) 的情况下获取某个半径(以米为单位)内的地点:
@RepositoryRestResource(collectionResourceRel = "places", path = "places")
public interface PlaceRepository extends JpaRepository<PlaceEntity, Long> {
@Query(value = "" +
"SELECT p " +
"FROM PlaceEntity p " +
"WHERE earth_distance( " +
" ll_to_earth(p.latitude, p.longitude), " +
" ll_to_earth(latitude, longitude) " +
") < radius")
List<PlaceEntity> findByLocationAndRadius(@Param("latitude") Float latitude,
@Param("longitude") Float longitude,
@Param("radius") Integer radius);
}
但是,运行它会抛出:
Caused by: org.postgresql.util.PSQLException: ERROR: function ll_to_earth(double precision, double precision) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Position: 343
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2284)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2003)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:200)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:424)
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:161)
at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:114)
at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52)
at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeQuery(HikariProxyPreparedStatement.java)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:60)
... 76 more
我也尝试更改设置nativeQuery = true 以及将@Query 更改为
@Query(value = "" +
"SELECT p " +
"FROM PlaceEntity p " +
"WHERE FUNCTION('earth_distance', " +
" FUNCTION('ll_to_earth', p.latitude, p.longitude), " +
" FUNCTION('ll_to_earth', latitude, longitude) " +
") < radius")
结果相同。
什么是正确的语法?
【问题讨论】:
标签: hibernate jpa spring-data-jpa