问题描述
我在存储库方法上使用 @Query
指定了Spring Data查询,但是它抛出了 NoViableAltException
异常.
I am specifying a Spring Data query using @Query
on a repository method but it is throwing a NoViableAltException
exception.
这是我正在使用的存储库接口方法和注释:
This is the repository interface method and annotation I am using:
@Query(value="SELECT one.saveLine, two.saveLine FROM (SELECT * FROM SaveOutputTable WHERE saveType = 'R' and seqId = '0' and executionId =:executionIdOne ) one JOIN (SELECT * FROM SaveOutputTable WHERE saveType = 'R' and seqId = '0' and executionId =:executionIdTwo) two ON one.lineId = two.lineId")
public List<ResultCompare> findByParamResult(@Param("executionIdOne") long executionIdOne,@Param("executionIdTwo") long executionIdTwo);
这会导致以下错误:
antlr.NoViableAltException: unexpected token: (
at org.hibernate.hql.internal.antlr.HqlBaseParser.fromRange(HqlBaseParser.java:1501) [hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.fromClause(HqlBaseParser.java:1325) [hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1045) [hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:730) [hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:323) [hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.hql.internal.antlr.HqlBaseParser.statement(HqlBaseParser.java:186) [hibernate-core-5.0.9.Final.jar:5.0.9.Final]
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:279) [hibernate-core-5.0.9.Final.jar:5.0.9.Final]
推荐答案
此错误与Hibernate完全无关.
This error has absolutely nothing to do with Hibernate.
您正在使用Spring Data批注 @Query
,该批注通常采用JPQL查询字符串.您指定的是本机查询SQL字符串,因此您需要修改批注并提供 nativeQuery = true
.
You are using the Spring Data annotation, @Query
which normally takes a JPQL query string. What you are specifying is a native query SQL string, so you need to modify the annotation and also provide nativeQuery=true
.
这篇关于将Spring数据查询与Hibernate一起使用时出现NoViableAltException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!