Parameter bind marker in query comments are not ignored by `ParameterBindingParser`
In the following code, the query has a question mark inside a comment, but this is causing a validation exception to be thrown.
interface MyRepository extends JpaRepository {
@Query("""
select *
from MyEntity /* TODO: should I use YourEntity? */
where id = :id
""")
Optional<MyEntity> findMyEntity(Long id);
}
Caused by: java.lang.IllegalArgumentException: Mixing of ? parameters and other forms like ?1 is not supported
at org.springframework.data.jpa.repository.query.StringQuery$ParameterBindingParser.parseParameterBindingsOfQueryIntoBindingsAndReturnCleanedQuery(StringQuery.java:286) ~[spring-data-jpa-3.3.11.jar:3.3.11]
at org.springframework.data.jpa.repository.query.StringQuery.<init>(StringQuery.java:85) ~[spring-data-jpa-3.3.11.jar:3.3.11]
at org.springframework.data.jpa.repository.query.DeclaredQuery.of(DeclaredQuery.java:40) ~[spring-data-jpa-3.3.11.jar:3.3.11]
at org.springframework.data.jpa.repository.query.JpaQueryMethod.assertParameterNamesInAnnotatedQuery(JpaQueryMethod.java:168) ~[spring-data-jpa-3.3.11.jar:3.3.11]
at org.springframework.data.jpa.repository.query.JpaQueryMethod.<init>(JpaQueryMethod.java:149) ~[spring-data-jpa-3.3.11.jar:3.3.11]
at org.springframework.data.jpa.repository.query.DefaultJpaQueryMethodFactory.build(DefaultJpaQueryMethodFactory.java:44) ~[spring-data-jpa-3.3.11.jar:3.3.11]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:94) ~[spring-data-jpa-3.3.11.jar:3.3.11]
at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.lookupQuery(QueryExecutorMethodInterceptor.java:115) ~[spring-data-commons-3.3.11.jar:3.3.11]
The validation should occur after the comments are stripped out.
Parameter pre-processing does not consider comments at all, block comments (/* … */, single-line comments (// …) or SQL-style (-- …) comments.
Ignoring comments requires covering the entire syntax space including quotations. That is a hard problem to solve. We would have to ignore comments with proper consideration of the grammar so that statements like WHERE id = '// ?' and foo = :bar and baz = ? or WHERE id = '/*' and foo = :bar and baz = ? and bar = '*/' match properly and the contents between e.g. /* and */ is not ignored (that is ignoring comment markers within literals and detect quotation bounds).
While your case is rather uncommon and causes clearly inconvenience, the complexity behind this issue is rather involved and so we leave things as they are for the time being.
Please also note that Spring Data JPA 3.3 is no longer supported, please refer to https://spring.io/projects/spring-data-jpa#support for maintained versions.