Hibernate Query查找两点之间的距离

时间:2015-03-15 08:08:34

标签: hibernate spring-mvc hql

我可以使用 -

找到距离
String hqlQuery = "SELECT *, (6371 * 2 * ASIN(SQRT(POWER(SIN((:ulatitude - abs(phy_1)) * pi()/180 / 2),2) +" +
                     "COS(:ulatitude * pi()/180 ) * COS(abs(phy_1) * pi()/180) *" +
                     "POWER(SIN((:ulongitude - phy_2) * pi()/180 / 2), 2)))) as d " +
                     "FROM location l HAVING distance < 10 ORDER BY d";
            Query query = getSession().createSQLQuery(hqlQuery);


            query.setParameter("ulatitude", latitude);
            query.setParameter("ulongitude", longitude);
            return query.list();

问题在于我的模型不包含作为场的距离。我无法弄明白 - 1.如何将其更改为正确的HQL而不是使用SQL查询。 2.如何返回此附加参数距离d,因为距离不是我的location模型中的字段。

我试过 -

String hql = "from location where (SELECT (6371 * 2 * ASIN(SQRT(POWER(SIN((:ulatitude - abs(phy_1)) * pi()/180 / 2),2) +" +
                     "COS(:ulatitude * pi()/180 ) * COS(abs(phy_1) * pi()/180) *" +
                     "POWER(SIN((:ulongitude - phy_2) * pi()/180 / 2), 2))))) < 10";

Query query = getSession().createQuery(hql);

但它会出现以下错误 -

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected end of subtree

1 个答案:

答案 0 :(得分:0)

我能够取消关注 -

String hql= "from location c where (6371 * 2 * ASIN(SQRT(POWER(SIN((:ulatitude - abs(c.phy_!)) * pi()/180 / 2),2) +" +
                     "COS(:ulatitude * pi()/180 ) * COS(abs(c.phy_1) * pi()/180) *" +
                     "POWER(SIN((:ulongitude - c.phy_2) * pi()/180 / 2), 2)))) < 10";