休眠异常.QuerySyntaxException:意外令牌:HAVING [英] Hibernate exception. QuerySyntaxException: unexpected token: HAVING

查看:61
本文介绍了休眠异常.QuerySyntaxException:意外令牌:HAVING的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用此HQL查询:

I try use this HQL query:

   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]

推荐答案

唯一方法是使用本机查询,因为:

The only way is to use a native query because :

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);

另一种方法是返回包含城市+距离的对象的列表,但是不可能在任何地方,但是您可以限制结果集

An alternative is to return a list of object containg the city + distance, but no where is possible, but you can limit the resultset

这篇关于休眠异常.QuerySyntaxException:意外令牌:HAVING的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆