21
21
22
22
import org .apache .commons .logging .Log ;
23
23
import org .apache .commons .logging .LogFactory ;
24
+
25
+ import org .springframework .core .env .StandardEnvironment ;
24
26
import org .springframework .data .jpa .repository .Query ;
25
27
import org .springframework .data .jpa .repository .QueryRewriter ;
26
28
import org .springframework .data .projection .ProjectionFactory ;
30
32
import org .springframework .data .repository .query .QueryLookupStrategy .Key ;
31
33
import org .springframework .data .repository .query .QueryMethod ;
32
34
import org .springframework .data .repository .query .QueryMethodEvaluationContextProvider ;
35
+ import org .springframework .data .repository .query .QueryMethodValueEvaluationContextAccessor ;
33
36
import org .springframework .data .repository .query .RepositoryQuery ;
37
+ import org .springframework .data .repository .query .ValueExpressionDelegate ;
34
38
import org .springframework .lang .Nullable ;
35
39
import org .springframework .util .Assert ;
36
40
import org .springframework .util .StringUtils ;
@@ -135,7 +139,7 @@ protected RepositoryQuery resolveQuery(JpaQueryMethod method, QueryRewriter quer
135
139
*/
136
140
private static class DeclaredQueryLookupStrategy extends AbstractQueryLookupStrategy {
137
141
138
- private final QueryMethodEvaluationContextProvider evaluationContextProvider ;
142
+ private final ValueExpressionDelegate valueExpressionDelegate ;
139
143
140
144
/**
141
145
* Creates a new {@link DeclaredQueryLookupStrategy}.
@@ -145,11 +149,11 @@ private static class DeclaredQueryLookupStrategy extends AbstractQueryLookupStra
145
149
* @param evaluationContextProvider must not be {@literal null}.
146
150
*/
147
151
public DeclaredQueryLookupStrategy (EntityManager em , JpaQueryMethodFactory queryMethodFactory ,
148
- QueryMethodEvaluationContextProvider evaluationContextProvider , QueryRewriterProvider queryRewriterProvider ) {
152
+ ValueExpressionDelegate delegate , QueryRewriterProvider queryRewriterProvider ) {
149
153
150
154
super (em , queryMethodFactory , queryRewriterProvider );
151
155
152
- this .evaluationContextProvider = evaluationContextProvider ;
156
+ this .valueExpressionDelegate = delegate ;
153
157
}
154
158
155
159
@ Override
@@ -168,13 +172,13 @@ protected RepositoryQuery resolveQuery(JpaQueryMethod method, QueryRewriter quer
168
172
}
169
173
170
174
return JpaQueryFactory .INSTANCE .fromMethodWithQueryString (method , em , method .getRequiredAnnotatedQuery (),
171
- getCountQuery (method , namedQueries , em ), queryRewriter , evaluationContextProvider );
175
+ getCountQuery (method , namedQueries , em ), queryRewriter , valueExpressionDelegate );
172
176
}
173
177
174
178
String name = method .getNamedQueryName ();
175
179
if (namedQueries .hasQuery (name )) {
176
180
return JpaQueryFactory .INSTANCE .fromMethodWithQueryString (method , em , namedQueries .getQuery (name ),
177
- getCountQuery (method , namedQueries , em ), queryRewriter , evaluationContextProvider );
181
+ getCountQuery (method , namedQueries , em ), queryRewriter , valueExpressionDelegate );
178
182
}
179
183
180
184
RepositoryQuery query = NamedQuery .lookupFrom (method , em );
@@ -267,28 +271,47 @@ protected RepositoryQuery resolveQuery(JpaQueryMethod method, QueryRewriter quer
267
271
* @param key may be {@literal null}.
268
272
* @param evaluationContextProvider must not be {@literal null}.
269
273
* @param escape must not be {@literal null}.
274
+ * @deprecated since 3.4, use
275
+ * {@link #create(EntityManager, JpaQueryMethodFactory, Key, ValueExpressionDelegate, QueryRewriterProvider, EscapeCharacter)}
276
+ * instead.
270
277
*/
278
+ @ Deprecated (since = "3.4" )
271
279
public static QueryLookupStrategy create (EntityManager em , JpaQueryMethodFactory queryMethodFactory ,
272
280
@ Nullable Key key , QueryMethodEvaluationContextProvider evaluationContextProvider ,
273
281
QueryRewriterProvider queryRewriterProvider , EscapeCharacter escape ) {
282
+ return create (em , queryMethodFactory , key ,
283
+ new ValueExpressionDelegate (new QueryMethodValueEvaluationContextAccessor (new StandardEnvironment (),
284
+ evaluationContextProvider .getEvaluationContextProvider ()), ValueExpressionDelegate .create ()),
285
+ queryRewriterProvider , escape );
286
+ }
287
+
288
+ /**
289
+ * Creates a {@link QueryLookupStrategy} for the given {@link EntityManager} and {@link Key}.
290
+ *
291
+ * @param em must not be {@literal null}.
292
+ * @param queryMethodFactory must not be {@literal null}.
293
+ * @param key may be {@literal null}.
294
+ * @param delegate must not be {@literal null}.
295
+ * @param queryRewriterProvider must not be {@literal null}.
296
+ * @param escape must not be {@literal null}.
297
+ */
298
+ public static QueryLookupStrategy create (EntityManager em , JpaQueryMethodFactory queryMethodFactory ,
299
+ @ Nullable Key key , ValueExpressionDelegate delegate , QueryRewriterProvider queryRewriterProvider ,
300
+ EscapeCharacter escape ) {
274
301
275
302
Assert .notNull (em , "EntityManager must not be null" );
276
- Assert .notNull (evaluationContextProvider , "EvaluationContextProvider must not be null" );
277
-
278
- switch (key != null ? key : Key .CREATE_IF_NOT_FOUND ) {
279
- case CREATE :
280
- return new CreateQueryLookupStrategy (em , queryMethodFactory , queryRewriterProvider , escape );
281
- case USE_DECLARED_QUERY :
282
- return new DeclaredQueryLookupStrategy (em , queryMethodFactory , evaluationContextProvider ,
283
- queryRewriterProvider );
284
- case CREATE_IF_NOT_FOUND :
285
- return new CreateIfNotFoundQueryLookupStrategy (em , queryMethodFactory ,
286
- new CreateQueryLookupStrategy (em , queryMethodFactory , queryRewriterProvider , escape ),
287
- new DeclaredQueryLookupStrategy (em , queryMethodFactory , evaluationContextProvider , queryRewriterProvider ),
288
- queryRewriterProvider );
289
- default :
290
- throw new IllegalArgumentException (String .format ("Unsupported query lookup strategy %s" , key ));
291
- }
303
+ Assert .notNull (delegate , "ValueExpressionDelegate must not be null" );
304
+
305
+ return switch (key != null ? key : Key .CREATE_IF_NOT_FOUND ) {
306
+ case CREATE -> new CreateQueryLookupStrategy (em , queryMethodFactory , queryRewriterProvider , escape );
307
+ case USE_DECLARED_QUERY ->
308
+ new DeclaredQueryLookupStrategy (em , queryMethodFactory , delegate , queryRewriterProvider );
309
+ case CREATE_IF_NOT_FOUND -> new CreateIfNotFoundQueryLookupStrategy (em , queryMethodFactory ,
310
+ new CreateQueryLookupStrategy (em , queryMethodFactory , queryRewriterProvider , escape ),
311
+ new DeclaredQueryLookupStrategy (em , queryMethodFactory , delegate , queryRewriterProvider ),
312
+ queryRewriterProvider );
313
+ default -> throw new IllegalArgumentException (String .format ("Unsupported query lookup strategy %s" , key ));
314
+ };
292
315
}
293
316
294
317
/**
0 commit comments