@@ -54,7 +54,7 @@ abstract class AbstractStringBasedJpaQuery extends AbstractJpaQuery {
54
54
private final SpelExpressionParser parser ;
55
55
private final QueryParameterSetter .QueryMetadataCache metadataCache = new QueryParameterSetter .QueryMetadataCache ();
56
56
private final QueryRewriter queryRewriter ;
57
- private ConcurrentLruCache < CachableQuery , String > queryCache = new ConcurrentLruCache <>( 16 , this :: applySorting ) ;
57
+ private final QuerySortRewriter querySortRewriter ;
58
58
private final Lazy <ParameterBinder > countParameterBinder ;
59
59
60
60
/**
@@ -102,6 +102,13 @@ public AbstractStringBasedJpaQuery(JpaQueryMethod method, EntityManager em, Stri
102
102
this .parser = parser ;
103
103
this .queryRewriter = queryRewriter ;
104
104
105
+ JpaParameters parameters = method .getParameters ();
106
+ if (parameters .hasPageableParameter () || parameters .hasSortParameter ()) {
107
+ this .querySortRewriter = new CachingQuerySortRewriter ();
108
+ } else {
109
+ this .querySortRewriter = NoOpQuerySortRewriter .INSTANCE ;
110
+ }
111
+
105
112
Assert .isTrue (method .isNativeQuery () || !query .usesJdbcStyleParameters (),
106
113
"JDBC style parameters (?) are not supported for JPA queries" );
107
114
}
@@ -110,7 +117,7 @@ public AbstractStringBasedJpaQuery(JpaQueryMethod method, EntityManager em, Stri
110
117
public Query doCreateQuery (JpaParametersParameterAccessor accessor ) {
111
118
112
119
Sort sort = accessor .getSort ();
113
- String sortedQueryString = applySortingIfNecessary (query , sort );
120
+ String sortedQueryString = querySortRewriter . getSorted (query , sort );
114
121
115
122
ResultProcessor processor = getQueryMethod ().getResultProcessor ().withDynamicProjection (accessor );
116
123
@@ -123,10 +130,6 @@ public Query doCreateQuery(JpaParametersParameterAccessor accessor) {
123
130
return parameterBinder .get ().bindAndPrepare (query , metadata , accessor );
124
131
}
125
132
126
- protected String applySorting (DeclaredQuery query , Sort sort ) {
127
- return queryCache .get (new CachableQuery (query , sort ));
128
- }
129
-
130
133
@ Override
131
134
protected ParameterBinder createBinder () {
132
135
return createBinder (query );
@@ -210,12 +213,46 @@ String applySorting(CachableQuery cachableQuery) {
210
213
cachableQuery .getAlias ());
211
214
}
212
215
213
- private String applySortingIfNecessary (DeclaredQuery query , Sort sort ) {
216
+ /**
217
+ * Query Sort Rewriter interface.
218
+ */
219
+ interface QuerySortRewriter {
220
+ String getSorted (DeclaredQuery query , Sort sort );
221
+ }
222
+
223
+ /**
224
+ * No-op query rewriter.
225
+ */
226
+ enum NoOpQuerySortRewriter implements QuerySortRewriter {
227
+ INSTANCE ;
228
+
229
+ public String getSorted (DeclaredQuery query , Sort sort ) {
230
+
231
+ if (sort .isSorted ()) {
232
+ throw new UnsupportedOperationException ("NoOpQueryCache does not support sorting" );
233
+ }
214
234
215
- if (sort .isUnsorted ()) {
216
235
return query .getQueryString ();
217
236
}
218
- return applySorting (query , sort );
237
+ }
238
+
239
+ /**
240
+ * Caching variant of {@link QuerySortRewriter}.
241
+ */
242
+ class CachingQuerySortRewriter implements QuerySortRewriter {
243
+
244
+ private final ConcurrentLruCache <CachableQuery , String > queryCache = new ConcurrentLruCache <>(16 ,
245
+ AbstractStringBasedJpaQuery .this ::applySorting );
246
+
247
+ @ Override
248
+ public String getSorted (DeclaredQuery query , Sort sort ) {
249
+
250
+ if (sort .isUnsorted ()) {
251
+ return query .getQueryString ();
252
+ }
253
+
254
+ return queryCache .get (new CachableQuery (query , sort ));
255
+ }
219
256
}
220
257
221
258
/**
@@ -227,7 +264,7 @@ private String applySortingIfNecessary(DeclaredQuery query, Sort sort) {
227
264
*/
228
265
static class CachableQuery {
229
266
230
- private DeclaredQuery declaredQuery ;
267
+ private final DeclaredQuery declaredQuery ;
231
268
private final String queryString ;
232
269
private final Sort sort ;
233
270
@@ -246,6 +283,7 @@ Sort getSort() {
246
283
return sort ;
247
284
}
248
285
286
+ @ Nullable
249
287
String getAlias () {
250
288
return declaredQuery .getAlias ();
251
289
}
0 commit comments