28
28
import org .springframework .data .repository .core .RepositoryMetadata ;
29
29
import org .springframework .data .repository .query .QueryLookupStrategy ;
30
30
import org .springframework .data .repository .query .QueryLookupStrategy .Key ;
31
+ import org .springframework .data .repository .query .QueryMethod ;
31
32
import org .springframework .data .repository .query .QueryMethodEvaluationContextProvider ;
32
33
import org .springframework .data .repository .query .RepositoryQuery ;
33
34
import org .springframework .lang .Nullable ;
@@ -47,6 +48,12 @@ public final class JpaQueryLookupStrategy {
47
48
48
49
private static final Log LOG = LogFactory .getLog (JpaQueryLookupStrategy .class );
49
50
51
+ /**
52
+ * A null-value instance used to signal if no declared query could be found. It checks many different formats before
53
+ * falling through to this value object.
54
+ */
55
+ private static final RepositoryQuery NO_QUERY = new NoQuery ();
56
+
50
57
/**
51
58
* Private constructor to prevent instantiation.
52
59
*/
@@ -172,12 +179,9 @@ protected RepositoryQuery resolveQuery(JpaQueryMethod method, QueryRewriter quer
172
179
173
180
RepositoryQuery query = NamedQuery .lookupFrom (method , em );
174
181
175
- if (null != query ) {
176
- return query ;
177
- }
178
-
179
- throw new IllegalStateException (
180
- String .format ("Did neither find a NamedQuery nor an annotated query for method %s!" , method ));
182
+ return query != null //
183
+ ? query //
184
+ : NO_QUERY ;
181
185
}
182
186
183
187
@ Nullable
@@ -245,11 +249,13 @@ public CreateIfNotFoundQueryLookupStrategy(EntityManager em, JpaQueryMethodFacto
245
249
protected RepositoryQuery resolveQuery (JpaQueryMethod method , QueryRewriter queryRewriter , EntityManager em ,
246
250
NamedQueries namedQueries ) {
247
251
248
- try {
249
- return lookupStrategy . resolveQuery ( method , queryRewriter , em , namedQueries );
250
- } catch ( IllegalStateException e ) {
251
- return createStrategy . resolveQuery ( method , queryRewriter , em , namedQueries ) ;
252
+ RepositoryQuery lookupQuery = lookupStrategy . resolveQuery ( method , queryRewriter , em , namedQueries );
253
+
254
+ if ( lookupQuery != NO_QUERY ) {
255
+ return lookupQuery ;
252
256
}
257
+
258
+ return createStrategy .resolveQuery (method , queryRewriter , em , namedQueries );
253
259
}
254
260
}
255
261
@@ -284,4 +290,20 @@ public static QueryLookupStrategy create(EntityManager em, JpaQueryMethodFactory
284
290
throw new IllegalArgumentException (String .format ("Unsupported query lookup strategy %s!" , key ));
285
291
}
286
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
+ }
287
309
}
0 commit comments