Question

I want to run below query of haversine formula as NamedQuery but I don't know how to correct it.

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

I run this query into mysql and it works fine for me but when I write below query as NamedQuery it gives me error:

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 )*])

Can anyone help me and tell me what's wrong with it?

Was it helpful?

Solution

Finally able to find out solution. Instead of createQuery, I used createNativeQuery and that solve my problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top