Reputation: 41
I am trying to execute JPA named query on my entity, but it is showing syntax error. Can anyone tell me what is the problem with my named query?
@Repository
public interface CollegeRepository extends CrudRepository<CollegeEntity, Integer> {
@Query("SELECT c FROM(SELECT *,(((acos(sin((:latitude * pi()/180)) * sin((latitude*pi()/180))+cos((:latitude * pi()/180)) * cos((latitude*pi()/180)) * cos(((:longitude - longitude)*pi()/180))))*180/pi())*60*1.1515*1.609344) as distance FROM college) t WHERE distance <= 30")
List<CollegeEntity> getCollegeByLocation(@Param("latitude") Double latitude, @Param("longitude") Double longitude, @Param("distance") Integer distance);
}
This is error after executing above query.
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: ( near line 1, column 14 [SELECT c FROM(SELECT *,(((acos(sin((:latitude * pi()/180)) * sin((latitude*pi()/180))+cos((:latitude * pi()/180)) * cos((latitude*pi()/180)) * cos(((:longitude - longitude)*pi()/180))))*180/pi())*60*1.1515*1.609344) as distance FROM college) t WHERE distance <= :distance]
When I execute below native query in MySQL terminal then working good.
mysql> SELECT * FROM(SELECT *,(((acos(sin((0.0 * pi()/180)) * sin((latitude*pi()/180))+cos((0.0 * pi()/180)) * cos((latitude*pi()/180)) * cos(((0.0 - longitude)*pi()/180))))*180/pi())*60*1.1515*1.609344) as distance FROM college) t WHERE distance <= 30;
Empty set (0.08 sec)
Upvotes: 0
Views: 368
Reputation: 59950
It seems that your query is not a correct JPQL Syntax, because there are no acos
, pi
, sin
, cod
functions in JPA, your query look like a native query, to solve your problem you have two options :
nativeQuery = true
attribute @Query(nativeQuery = true, value="SELECT * FROM(...")
, note the *
there are no alias c
in your query.You can use :
@Query(value = "SELECT * FROM(SELECT *,"
+ "(((acos(sin((0.0 * pi()/180)) * sin((latitude*pi()/180)) + "
+ "cos((0.0 * pi()/180)) * cos((latitude*pi()/180)) * "
+ "cos(((0.0 - longitude)*pi()/180))))*180/pi()) * "
+ "60*1.1515*1.609344) as distance FROM college) t WHERE distance <= 30;",
nativeQuery = true)
In your case, I strongly suggest the second option.
Upvotes: 1