spring-projects / spring-data-jpa

Simplifies the development of creating a JPA-based data access layer.
https://spring.io/projects/spring-data-jpa/
Apache License 2.0
2.93k stars 1.39k forks source link

Spring Data JPA generates incorrect JPQL query for sorted pagination request with UNION clause #3427

Open Pedroloma opened 2 months ago

Pedroloma commented 2 months ago

After updating my app from 3.0.8 to 3.1.10, I get an exception for a JPL with UNION clauses, when the query is pageable and sorted.

I've created a GitHub repo where you can reproduce the issue: https://github.com/Pedroloma/JpaWithUnionAndPageableAndSortedApplication

I think, that this issue could be related to this other one: https://github.com/spring-projects/spring-data-jpa/issues/2969

This is the exception thrown and its stack trace:

Caused by: org.hibernate.query.sqm.ParsingException: line 1:79 mismatched input 'ascUNION' expecting {<EOF>, ',', '.', '[', '+', '-', '*', '/', '%', '||', ASC, BY, DAY, DESC, EPOCH, FETCH, HOUR, LIMIT, MINUTE, MONTH, NANOSECOND, NULLS, OFFSET, QUARTER, SECOND, WEEK, YEAR}
    at org.hibernate.query.hql.internal.StandardHqlTranslator$1.syntaxError(StandardHqlTranslator.java:46)
    at org.antlr.v4.runtime.ProxyErrorListener.syntaxError(ProxyErrorListener.java:41)
    at org.antlr.v4.runtime.Parser.notifyErrorListeners(Parser.java:543)
    at org.antlr.v4.runtime.DefaultErrorStrategy.reportInputMismatch(DefaultErrorStrategy.java:327)
    at org.antlr.v4.runtime.DefaultErrorStrategy.reportError(DefaultErrorStrategy.java:139)
    at org.hibernate.grammars.hql.HqlParser.statement(HqlParser.java:361)
    at org.hibernate.query.hql.internal.StandardHqlTranslator.parseHql(StandardHqlTranslator.java:127)
    at org.hibernate.query.hql.internal.StandardHqlTranslator.translate(StandardHqlTranslator.java:77)
    at org.hibernate.query.internal.QueryInterpretationCacheStandardImpl.createHqlInterpretation(QueryInterpretationCacheStandardImpl.java:165)
    at org.hibernate.query.internal.QueryInterpretationCacheStandardImpl.resolveHqlInterpretation(QueryInterpretationCacheStandardImpl.java:147)
    at org.hibernate.internal.AbstractSharedSessionContract.interpretHql(AbstractSharedSessionContract.java:741)
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:786)
    ... 212 common frames omitted

Please, let me know if you guys need some extra information.

Thanks in advance.

christophstrobl commented 2 months ago

@Pedroloma thanks for reporting, we'll look into this.