我想在 haversine 公式的查询下面作为 NamedQuery 运行,但我不知道如何更正它。
set @orig_lat = 37.334542;
set @orig_lon = -121.890821;
set @dist = 10;
select *,
3956 * 2 * ASIN(SQRT(POWER(SIN((@orig_lat - abs(mlatitude)) * pi() / 180 / 2), 2)
+ COS(@orig_lat * pi() / 180) * COS(abs(mlatitude) * pi() / 180) * POWER(SIN((@orig_lon - mlogitude) * pi() / 180 / 2), 2))) as distance
from user_gps_location
having distance < @dist
ORDER BY distance
我将这个查询运行到 mysql 中,它对我来说工作正常,但是当我将下面的查询写为 NamedQuery 时,它给我错误:
UserGpsLocation users = (UserGpsLocation)em.createQuery("select (3956*2*ASIN(SQRT(POWER(SIN((?1-abs(u.mlatitude))*pi()/180/2),2)+COS(?1*pi()/180) * COS(abs(u.mlatitude)* pi()/180) *POWER(SIN((?2 -u.mlogitude)* pi()/180/2),2)))) as distance from UserGpsLocation u having distance < :dist ORDER BY distance")
.setParameter(1, mlatitude)
.setParameter(2, mlogitude)
.setParameter("dist", 10)
.getResultList();
异常(exception):
javax.servlet.ServletException: java.lang.IllegalArgumentException: An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing the query [select (3956*2*ASIN(SQRT(POWER(SIN((?1-abs(u.mlatitude))*pi()/180/2),2)+COS(?1*pi()/180) * COS(abs(u.mlatitude)* pi()/180) *POWER(SIN((?2 -u.mlogitude)* pi()/180/2),2)))) as distance from UserGpsLocation u having distance < :dist ORDER BY distance], line 1, column 19: unexpected token [(].
Internal Exception: NoViableAltException(83@[()* loopback of 383:9: (d= DOT right= attribute )*])
任何人都可以帮助我并告诉我它有什么问题吗?
最佳答案
终于找到解决办法了。我没有使用 createQuery,而是使用了 createNativeQuery,这解决了我的问题。
关于java - 如何在NamedQuery中编写NamedQuery haversine公式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11657256/