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 );
@@ -271,20 +275,39 @@ protected RepositoryQuery resolveQuery(JpaQueryMethod method, QueryRewriter quer
271
275
public static QueryLookupStrategy create (EntityManager em , JpaQueryMethodFactory queryMethodFactory ,
272
276
@ Nullable Key key , QueryMethodEvaluationContextProvider evaluationContextProvider ,
273
277
QueryRewriterProvider queryRewriterProvider , EscapeCharacter escape ) {
278
+ return create (em , queryMethodFactory , key ,
279
+ new ValueExpressionDelegate (new QueryMethodValueEvaluationContextAccessor (new StandardEnvironment (),
280
+ evaluationContextProvider .getEvaluationContextProvider ()), ValueExpressionDelegate .create ()),
281
+ queryRewriterProvider , escape );
282
+ }
283
+
284
+ /**
285
+ * Creates a {@link QueryLookupStrategy} for the given {@link EntityManager} and {@link Key}.
286
+ *
287
+ * @param em must not be {@literal null}.
288
+ * @param queryMethodFactory must not be {@literal null}.
289
+ * @param key may be {@literal null}.
290
+ * @param delegate must not be {@literal null}.
291
+ * @param queryRewriterProvider must not be {@literal null}.
292
+ * @param escape must not be {@literal null}.
293
+ */
294
+ public static QueryLookupStrategy create (EntityManager em , JpaQueryMethodFactory queryMethodFactory ,
295
+ @ Nullable Key key , ValueExpressionDelegate delegate , QueryRewriterProvider queryRewriterProvider ,
296
+ EscapeCharacter escape ) {
274
297
275
298
Assert .notNull (em , "EntityManager must not be null" );
276
- Assert .notNull (evaluationContextProvider , "EvaluationContextProvider must not be null" );
299
+ Assert .notNull (delegate , "ValueExpressionDelegate must not be null" );
277
300
278
301
switch (key != null ? key : Key .CREATE_IF_NOT_FOUND ) {
279
302
case CREATE :
280
303
return new CreateQueryLookupStrategy (em , queryMethodFactory , queryRewriterProvider , escape );
281
304
case USE_DECLARED_QUERY :
282
- return new DeclaredQueryLookupStrategy (em , queryMethodFactory , evaluationContextProvider ,
305
+ return new DeclaredQueryLookupStrategy (em , queryMethodFactory , delegate ,
283
306
queryRewriterProvider );
284
307
case CREATE_IF_NOT_FOUND :
285
308
return new CreateIfNotFoundQueryLookupStrategy (em , queryMethodFactory ,
286
309
new CreateQueryLookupStrategy (em , queryMethodFactory , queryRewriterProvider , escape ),
287
- new DeclaredQueryLookupStrategy (em , queryMethodFactory , evaluationContextProvider , queryRewriterProvider ),
310
+ new DeclaredQueryLookupStrategy (em , queryMethodFactory , delegate , queryRewriterProvider ),
288
311
queryRewriterProvider );
289
312
default :
290
313
throw new IllegalArgumentException (String .format ("Unsupported query lookup strategy %s" , key ));
0 commit comments