使用Spring Data查询和Hibernate时的NoViableAltException

时间:2017-02-09 19:29:32

标签: java hibernate spring-data spring-data-jpa

我在存储库方法上使用@Query指定Spring Data查询,但它抛出了NoViableAltException异常。

这是我正在使用的存储库接口方法和注释:

@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]

1 个答案:

答案 0 :(得分:3)

此错误与Hibernate完全无关。

您正在使用Spring数据注释declare @table table (PersonID int, Status varchar(16), WhenChanged varchar(64)) insert into @table values (101,'New','27/01/2017 15:27'), (101,'New','27/01/2017 16:40'), (101,'New','27/01/2017 16:40'), (101,'Pending','27/01/2017 16:40'), (101,'Pending','27/01/2017 16:40'), (101,'Pending','27/01/2017 16:40'), (101,'New','31/01/2017 09:14'), (101,'New','31/01/2017 10:02'), (101,'New','31/01/2017 10:03'), (101,'New','31/01/2017 10:05'), (101,'Pending','03/02/2017 14:29'), (101,'Pending','03/02/2017 14:29') ;with cte as( select PersonID, Status, convert(datetime,WhenChanged,103) as WhenChanged, row_number() over (order by personID, convert(datetime,WhenChanged,103)) as RN from @table), cteResults as( select PersonID, Status, WhenChanged from cte where RN = 1 UNION select c.PersonID, c.Status, c.WhenChanged from cte c inner join cte c2 on c2.rn = (c.rn -1) and c2.status <> c.status) select * from cteResults order by WhenChanged ,它通常采用JPQL查询字符串。您指定的是本机查询SQL字符串,因此您需要修改注释并提供@Query