21
21
22
22
import org .slf4j .Logger ;
23
23
import org .slf4j .LoggerFactory ;
24
-
25
24
import org .springframework .data .jpa .repository .Query ;
26
25
import org .springframework .data .projection .ProjectionFactory ;
27
26
import org .springframework .data .repository .core .NamedQueries ;
28
27
import org .springframework .data .repository .core .RepositoryMetadata ;
29
28
import org .springframework .data .repository .query .QueryLookupStrategy ;
30
29
import org .springframework .data .repository .query .QueryLookupStrategy .Key ;
30
+ import org .springframework .data .repository .query .QueryMethod ;
31
31
import org .springframework .data .repository .query .QueryMethodEvaluationContextProvider ;
32
32
import org .springframework .data .repository .query .RepositoryQuery ;
33
33
import org .springframework .lang .Nullable ;
@@ -46,6 +46,12 @@ public final class JpaQueryLookupStrategy {
46
46
47
47
private static final Logger LOG = LoggerFactory .getLogger (JpaQueryLookupStrategy .class );
48
48
49
+ /**
50
+ * A null-value instance used to signal if no declared query could be found. It checks many different formats before
51
+ * falling through to this value object.
52
+ */
53
+ private static final RepositoryQuery NO_QUERY = new NoQuery ();
54
+
49
55
/**
50
56
* Private constructor to prevent instantiation.
51
57
*/
@@ -161,24 +167,20 @@ protected RepositoryQuery resolveQuery(JpaQueryMethod method, EntityManager em,
161
167
}
162
168
163
169
return JpaQueryFactory .INSTANCE .fromMethodWithQueryString (method , em , method .getRequiredAnnotatedQuery (),
164
- getCountQuery (method , namedQueries , em ),
165
- evaluationContextProvider );
170
+ getCountQuery (method , namedQueries , em ), evaluationContextProvider );
166
171
}
167
172
168
173
String name = method .getNamedQueryName ();
169
174
if (namedQueries .hasQuery (name )) {
170
- return JpaQueryFactory .INSTANCE .fromMethodWithQueryString (method , em , namedQueries .getQuery (name ), getCountQuery ( method , namedQueries , em ),
171
- evaluationContextProvider );
175
+ return JpaQueryFactory .INSTANCE .fromMethodWithQueryString (method , em , namedQueries .getQuery (name ),
176
+ getCountQuery ( method , namedQueries , em ), evaluationContextProvider );
172
177
}
173
178
174
179
RepositoryQuery query = NamedQuery .lookupFrom (method , em );
175
180
176
- if (null != query ) {
177
- return query ;
178
- }
179
-
180
- throw new IllegalStateException (
181
- String .format ("Did neither find a NamedQuery nor an annotated query for method %s!" , method ));
181
+ return query != null //
182
+ ? query //
183
+ : NO_QUERY ;
182
184
}
183
185
184
186
@ Nullable
@@ -248,11 +250,13 @@ public CreateIfNotFoundQueryLookupStrategy(EntityManager em, JpaQueryMethodFacto
248
250
@ Override
249
251
protected RepositoryQuery resolveQuery (JpaQueryMethod method , EntityManager em , NamedQueries namedQueries ) {
250
252
251
- try {
252
- return lookupStrategy . resolveQuery ( method , em , namedQueries );
253
- } catch ( IllegalStateException e ) {
254
- return createStrategy . resolveQuery ( method , em , namedQueries ) ;
253
+ RepositoryQuery lookupQuery = lookupStrategy . resolveQuery ( method , em , namedQueries );
254
+
255
+ if ( lookupQuery != NO_QUERY ) {
256
+ return lookupQuery ;
255
257
}
258
+
259
+ return createStrategy .resolveQuery (method , em , namedQueries );
256
260
}
257
261
}
258
262
@@ -286,4 +290,20 @@ public static QueryLookupStrategy create(EntityManager em, JpaQueryMethodFactory
286
290
throw new IllegalArgumentException (String .format ("Unsupported query lookup strategy %s!" , key ));
287
291
}
288
292
}
293
+
294
+ /**
295
+ * A null value type that represents the lack of a defined query.
296
+ */
297
+ static class NoQuery implements RepositoryQuery {
298
+
299
+ @ Override
300
+ public Object execute (Object [] parameters ) {
301
+ throw new IllegalStateException ("NoQuery should not be executed!" );
302
+ }
303
+
304
+ @ Override
305
+ public QueryMethod getQueryMethod () {
306
+ throw new IllegalStateException ("NoQuery does not have a QueryMethod!" );
307
+ }
308
+ }
289
309
}
0 commit comments