我尝试使用这个HQL查询:
Result.find("SELECT c, ( 3959 * acos( cos( radians(?) ) * "+
"cos( radians( c.latitude ) ) *"+
"cos( radians( c.longitude ) - radians(?) ) +"+
"sin( radians(?) ) * sin( radians( c.latitude ) ) ) ) " +
"AS distance FROM City c HAVING distance < ? ORDER BY distance ASC",
latitude, longitude, latitude, radius).fetch();
但结果是:
IllegalArgumentException occured : org.hibernate.hql.ast.QuerySyntaxException: unexpected token: HAVING near line 1, column 204 [SELECT c, ( 3959 * acos( cos( radians(?) ) * cos( radians( c.latitude ) ) *cos( radians( c.longitude ) - radians(?) ) +sin( radians(?) ) * sin( radians( c.latitude ) ) ) ) AS distance FROM models.City c HAVING distance < ? ORDER BY distance ASC]
答案 0 :(得分:1)
尝试在您的查询中将HAVING
更改为WHERE
。
答案 1 :(得分:1)
仅当您使用GROUP BY时才允许使用关键字HAVING(想想对于GROUPS使用WHERE)。 有关SELECT语法,请参阅reference manual。
答案 2 :(得分:0)
嗯,应该是什么“?”参数β
如果是,你应该像
一样使用它HAVING distance < :distance
并使用
query.setParameter("distance", 50);
答案 3 :(得分:0)
试
select dis.c,dis.distance(
SELECT c,
( 3959 * acos( cos( radians(?) ) * cos( radians( c.latitude ) ) *
cos( radians( c.longitude ) - radians(?) ) + sin( radians(?) ) *
sin( radians( c.latitude ) ) ) ) AS distance FROM City c ) dis
where dis.distance<? order by dis.distance ASC
答案 4 :(得分:0)
唯一的方法是使用本机查询,因为:
Java代码:
@Query(value = "SELECT s.*,\n" +
" (\n" +
" 3959 * acos(\n" +
" cos(radians(s.lat))\n" +
" * cos(radians(:lat))\n" +
" * cos(radians(:lon) - radians(s.lon))\n" +
" + sin(radians(s.lat))\n" +
" * sin(radians(:lat))\n" +
" )\n" +
" ) AS distance\n" +
"FROM Stop s\n" +
"HAVING distance < 30\n" +
"ORDER BY distance asc", nativeQuery = true)
List<Stop> findClosestStops(@Param("lat") Double lat, @Param("lon") Double lon);
另一种方法是返回一个包含城市+距离的对象列表,但是没有可能的地方,但你可以限制结果集