0

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)
6
  • User has 2 queries posted and doesn't say which one they refer to, nor what the "syntax error" is. Also there is no JPA "named query" there. You have 2 SPRING DATA JPA queries Commented Jan 14, 2018 at 7:33
  • Sorry, I will edit my question. Commented Jan 14, 2018 at 7:35
  • I have edited my question, Thanks for suggestion Commented Jan 14, 2018 at 7:38
  • so it isnt valid JPQL, as you have been told on the answer Commented Jan 14, 2018 at 7:40
  • Yes, it is not valid JPQL, you can see error, I have edited answer with error too. Commented Jan 14, 2018 at 7:44

1 Answer 1

1

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 :

  • First option : Convert your query to a correct JPQL syntax, you can follow the documentation
  • Second option : use 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.

Sign up to request clarification or add additional context in comments.

4 Comments

I used the second option, But there is error showing "Invalid MemberValuePair". Any suggestion?
@SagarYadav can you edit your question and add the full error, beside focus on one error per question please
I am accepting your answer. But, before that can you tell me what can be JPQL alternative? If the JPQL option is too difficult, can you describe why you strongly suggest the second option?
@SagarYadav i already mention that, because you are using some function not exist in JPQL, unless you go with other options like eclipse.org/eclipselink/documentation/2.4/jpa/extensions/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.