2012-07-25 25 views
1

我想運行下面的haversine公式查詢作爲NamedQuery,但我不知道如何糾正它。如何在NamedQuery中編寫NamedQuery haversine公式?

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

例外:

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([email protected][()* loopback of 383:9: (d= DOT right= attribute)*]) 

人幫助我,告訴我它有什麼問題?

+0

如果您重新登錄,可能會有所幫助,例如[sql]爲[java]? – Josien 2012-07-26 11:37:33

回答

0

最後能夠找出解決方案。而不是createQuery,我使用createNativeQuery並解決了我的問題。