@@ -190,6 +190,42 @@ void noQueryShouldNotBeInvoked() {
190
190
assertThatIllegalStateException ().isThrownBy (() -> query .getQueryMethod ());
191
191
}
192
192
193
+ @ Test // GH-2551
194
+ void customQueryWithQuestionMarksShouldWork () throws NoSuchMethodException {
195
+
196
+ QueryLookupStrategy strategy = JpaQueryLookupStrategy .create (em , queryMethodFactory , Key .CREATE_IF_NOT_FOUND ,
197
+ EVALUATION_CONTEXT_PROVIDER , EscapeCharacter .DEFAULT );
198
+
199
+ Method namedMethod = UserRepository .class .getMethod ("customQueryWithQuestionMarksAndNamedParam" , String .class );
200
+ RepositoryMetadata namedMetadata = new DefaultRepositoryMetadata (UserRepository .class );
201
+
202
+ strategy .resolveQuery (namedMethod , namedMetadata , projectionFactory , namedQueries );
203
+
204
+ assertThatIllegalArgumentException ().isThrownBy (() -> {
205
+
206
+ Method jdbcStyleMethod = UserRepository .class .getMethod ("customQueryWithQuestionMarksAndJdbcStyleParam" ,
207
+ String .class );
208
+ RepositoryMetadata jdbcStyleMetadata = new DefaultRepositoryMetadata (UserRepository .class );
209
+
210
+ strategy .resolveQuery (jdbcStyleMethod , jdbcStyleMetadata , projectionFactory , namedQueries );
211
+ }).withMessageContaining ("JDBC style parameters (?) are not supported for JPA queries." );
212
+
213
+ Method jpaStyleMethod = UserRepository .class .getMethod ("customQueryWithQuestionMarksAndNumberedStyleParam" ,
214
+ String .class );
215
+ RepositoryMetadata jpaStyleMetadata = new DefaultRepositoryMetadata (UserRepository .class );
216
+
217
+ strategy .resolveQuery (jpaStyleMethod , jpaStyleMetadata , projectionFactory , namedQueries );
218
+
219
+ assertThatIllegalArgumentException ().isThrownBy (() -> {
220
+
221
+ Method jpaAndJdbcStyleMethod = UserRepository .class
222
+ .getMethod ("customQueryWithQuestionMarksAndJdbcStyleAndNumberedStyleParam" , String .class , String .class );
223
+ RepositoryMetadata jpaAndJdbcMetadata = new DefaultRepositoryMetadata (UserRepository .class );
224
+
225
+ strategy .resolveQuery (jpaAndJdbcStyleMethod , jpaAndJdbcMetadata , projectionFactory , namedQueries );
226
+ }).withMessageContaining ("Mixing of ? parameters and other forms like ?1 is not supported" );
227
+ }
228
+
193
229
interface UserRepository extends Repository <User , Integer > {
194
230
195
231
@ Query ("something absurd" )
@@ -207,6 +243,18 @@ interface UserRepository extends Repository<User, Integer> {
207
243
@ Query (value = "something absurd" , name = "my-query-name" )
208
244
User annotatedQueryWithQueryAndQueryName ();
209
245
246
+ @ Query ("SELECT * FROM table WHERE (json_col->'jsonKey')::jsonb \\ ?\\ ? :param " )
247
+ List <User > customQueryWithQuestionMarksAndNamedParam (String param );
248
+
249
+ @ Query ("SELECT * FROM table WHERE (json_col->'jsonKey')::jsonb \\ ?\\ ? ? " )
250
+ List <User > customQueryWithQuestionMarksAndJdbcStyleParam (String param );
251
+
252
+ @ Query ("SELECT * FROM table WHERE (json_col->'jsonKey')::jsonb \\ ?\\ ? ?1 " )
253
+ List <User > customQueryWithQuestionMarksAndNumberedStyleParam (String param );
254
+
255
+ @ Query ("SELECT * FROM table WHERE (json_col->'jsonKey')::jsonb \\ ?\\ ? ?1 and other_col = ? " )
256
+ List <User > customQueryWithQuestionMarksAndJdbcStyleAndNumberedStyleParam (String param1 , String param2 );
257
+
210
258
// This is a named query with Sort parameter, which isn't supported
211
259
List <User > customNamedQuery (String firstname , Sort sort );
212
260
}
0 commit comments