java 在 Hibernate 中使用 Spring Data 查询时出现 NoViableAltException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42145271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NoViableAltException while using Spring Data query with Hibernate
提问by Goutam
I am specifying a Spring Data query using @Query
on a repository method but it is throwing a NoViableAltException
exception.
我正在使用@Query
存储库方法指定 Spring Data 查询,但它引发了NoViableAltException
异常。
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);
This causes the following error:
这会导致以下错误:
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]
回答by Naros
This error has absolutely nothing to do with Hibernate.
此错误与 Hibernate 完全无关。
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 Data 注释,@Query
它通常采用 JPQL 查询字符串。您指定的是本机查询 SQL 字符串,因此您需要修改注释并提供nativeQuery=true
.