简体   繁体   English

JPA命名查询显示错误

[英]JPA Named Query showing error

I am trying to execute JPA named query on my entity, but it is showing syntax error. 我试图在我的实体上执行JPA命名查询,但是它显示语法错误。 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终端中的本地查询下执行时,工作正常。

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)

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 : 您的查询似乎不是正确的JPQL语法,因为JPA中没有acospisincod函数,因此您的查询看起来像是本机查询,您可以通过以下两种方法来解决问题:

  • First option : Convert your query to a correct JPQL syntax, you can follow the documentation 第一种选择:将查询转换为正确的JPQL语法,可以按照文档进行操作
  • Second option : use nativeQuery = true attribute @Query(nativeQuery = true, value="SELECT * FROM(...") , note the * there are no alias c in your query. 第二种选择:使用nativeQuery = true属性@Query(nativeQuery = true, value="SELECT * FROM(...") ,请注意*您的查询中没有别名c

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. 对于您的情况,我强烈建议第二种选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM