15
15
*/
16
16
package org .springframework .data .jpa .repository .query ;
17
17
18
- import java .lang .reflect .Method ;
19
-
20
18
import jakarta .persistence .EntityManager ;
21
19
20
+ import java .lang .reflect .Method ;
21
+
22
22
import org .apache .commons .logging .Log ;
23
23
import org .apache .commons .logging .LogFactory ;
24
-
24
+ import org . springframework . beans . factory . BeanFactory ;
25
25
import org .springframework .data .jpa .repository .Query ;
26
26
import org .springframework .data .projection .ProjectionFactory ;
27
27
import org .springframework .data .repository .core .NamedQueries ;
@@ -61,20 +61,23 @@ private abstract static class AbstractQueryLookupStrategy implements QueryLookup
61
61
62
62
private final EntityManager em ;
63
63
private final JpaQueryMethodFactory queryMethodFactory ;
64
+ private final BeanFactory beanFactory ;
64
65
65
66
/**
66
67
* Creates a new {@link AbstractQueryLookupStrategy}.
67
68
*
68
69
* @param em must not be {@literal null}.
69
70
* @param queryMethodFactory must not be {@literal null}.
70
71
*/
71
- public AbstractQueryLookupStrategy (EntityManager em , JpaQueryMethodFactory queryMethodFactory ) {
72
+ public AbstractQueryLookupStrategy (EntityManager em , JpaQueryMethodFactory queryMethodFactory ,
73
+ BeanFactory beanFactory ) {
72
74
73
75
Assert .notNull (em , "EntityManager must not be null!" );
74
76
Assert .notNull (queryMethodFactory , "JpaQueryMethodFactory must not be null!" );
75
77
76
78
this .em = em ;
77
79
this .queryMethodFactory = queryMethodFactory ;
80
+ this .beanFactory = beanFactory ;
78
81
}
79
82
80
83
@ Override
@@ -84,6 +87,10 @@ public final RepositoryQuery resolveQuery(Method method, RepositoryMetadata meta
84
87
}
85
88
86
89
protected abstract RepositoryQuery resolveQuery (JpaQueryMethod method , EntityManager em , NamedQueries namedQueries );
90
+
91
+ protected BeanFactory getBeanFactory () {
92
+ return beanFactory ;
93
+ }
87
94
}
88
95
89
96
/**
@@ -97,9 +104,9 @@ private static class CreateQueryLookupStrategy extends AbstractQueryLookupStrate
97
104
private final EscapeCharacter escape ;
98
105
99
106
public CreateQueryLookupStrategy (EntityManager em , JpaQueryMethodFactory queryMethodFactory ,
100
- EscapeCharacter escape ) {
107
+ BeanFactory beanFactory , EscapeCharacter escape ) {
101
108
102
- super (em , queryMethodFactory );
109
+ super (em , queryMethodFactory , beanFactory );
103
110
104
111
this .escape = escape ;
105
112
}
@@ -130,9 +137,9 @@ private static class DeclaredQueryLookupStrategy extends AbstractQueryLookupStra
130
137
* @param evaluationContextProvider must not be {@literal null}.
131
138
*/
132
139
public DeclaredQueryLookupStrategy (EntityManager em , JpaQueryMethodFactory queryMethodFactory ,
133
- QueryMethodEvaluationContextProvider evaluationContextProvider ) {
140
+ QueryMethodEvaluationContextProvider evaluationContextProvider , BeanFactory beanFactory ) {
134
141
135
- super (em , queryMethodFactory );
142
+ super (em , queryMethodFactory , beanFactory );
136
143
137
144
this .evaluationContextProvider = evaluationContextProvider ;
138
145
}
@@ -152,14 +159,13 @@ protected RepositoryQuery resolveQuery(JpaQueryMethod method, EntityManager em,
152
159
}
153
160
154
161
return JpaQueryFactory .INSTANCE .fromMethodWithQueryString (method , em , method .getRequiredAnnotatedQuery (),
155
- getCountQuery (method , namedQueries , em ),
156
- evaluationContextProvider );
162
+ getCountQuery (method , namedQueries , em ), evaluationContextProvider , getBeanFactory ());
157
163
}
158
164
159
165
String name = method .getNamedQueryName ();
160
166
if (namedQueries .hasQuery (name )) {
161
- return JpaQueryFactory .INSTANCE .fromMethodWithQueryString (method , em , namedQueries .getQuery (name ), getCountQuery ( method , namedQueries , em ),
162
- evaluationContextProvider );
167
+ return JpaQueryFactory .INSTANCE .fromMethodWithQueryString (method , em , namedQueries .getQuery (name ),
168
+ getCountQuery ( method , namedQueries , em ), evaluationContextProvider , getBeanFactory () );
163
169
}
164
170
165
171
RepositoryQuery query = NamedQuery .lookupFrom (method , em );
@@ -221,9 +227,9 @@ private static class CreateIfNotFoundQueryLookupStrategy extends AbstractQueryLo
221
227
* @param lookupStrategy must not be {@literal null}.
222
228
*/
223
229
public CreateIfNotFoundQueryLookupStrategy (EntityManager em , JpaQueryMethodFactory queryMethodFactory ,
224
- CreateQueryLookupStrategy createStrategy , DeclaredQueryLookupStrategy lookupStrategy ) {
230
+ CreateQueryLookupStrategy createStrategy , DeclaredQueryLookupStrategy lookupStrategy , BeanFactory beanFactory ) {
225
231
226
- super (em , queryMethodFactory );
232
+ super (em , queryMethodFactory , beanFactory );
227
233
228
234
Assert .notNull (createStrategy , "CreateQueryLookupStrategy must not be null!" );
229
235
Assert .notNull (lookupStrategy , "DeclaredQueryLookupStrategy must not be null!" );
@@ -253,20 +259,22 @@ protected RepositoryQuery resolveQuery(JpaQueryMethod method, EntityManager em,
253
259
* @param escape must not be {@literal null}.
254
260
*/
255
261
public static QueryLookupStrategy create (EntityManager em , JpaQueryMethodFactory queryMethodFactory ,
256
- @ Nullable Key key , QueryMethodEvaluationContextProvider evaluationContextProvider , EscapeCharacter escape ) {
262
+ @ Nullable Key key , QueryMethodEvaluationContextProvider evaluationContextProvider , BeanFactory beanFactory ,
263
+ EscapeCharacter escape ) {
257
264
258
265
Assert .notNull (em , "EntityManager must not be null!" );
259
266
Assert .notNull (evaluationContextProvider , "EvaluationContextProvider must not be null!" );
260
267
261
268
switch (key != null ? key : Key .CREATE_IF_NOT_FOUND ) {
262
269
case CREATE :
263
- return new CreateQueryLookupStrategy (em , queryMethodFactory , escape );
270
+ return new CreateQueryLookupStrategy (em , queryMethodFactory , beanFactory , escape );
264
271
case USE_DECLARED_QUERY :
265
- return new DeclaredQueryLookupStrategy (em , queryMethodFactory , evaluationContextProvider );
272
+ return new DeclaredQueryLookupStrategy (em , queryMethodFactory , evaluationContextProvider , beanFactory );
266
273
case CREATE_IF_NOT_FOUND :
267
274
return new CreateIfNotFoundQueryLookupStrategy (em , queryMethodFactory ,
268
- new CreateQueryLookupStrategy (em , queryMethodFactory , escape ),
269
- new DeclaredQueryLookupStrategy (em , queryMethodFactory , evaluationContextProvider ));
275
+ new CreateQueryLookupStrategy (em , queryMethodFactory , beanFactory , escape ),
276
+ new DeclaredQueryLookupStrategy (em , queryMethodFactory , evaluationContextProvider , beanFactory ),
277
+ beanFactory );
270
278
default :
271
279
throw new IllegalArgumentException (String .format ("Unsupported query lookup strategy %s!" , key ));
272
280
}
0 commit comments