Skip to content

Commit 66ca3ca

Browse files
committed
Polishing.
Make query factory methods non-nullable by moving conditionals to the lookup strategy. See #2217
1 parent d5cc729 commit 66ca3ca

File tree

2 files changed

+8
-26
lines changed

2 files changed

+8
-26
lines changed

src/main/java/org/springframework/data/jpa/repository/query/JpaQueryFactory.java

+1-16
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717

1818
import javax.persistence.EntityManager;
1919

20-
import org.slf4j.Logger;
21-
import org.slf4j.LoggerFactory;
22-
2320
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
2421
import org.springframework.data.repository.query.RepositoryQuery;
2522
import org.springframework.expression.spel.standard.SpelExpressionParser;
@@ -36,7 +33,6 @@ enum JpaQueryFactory {
3633
INSTANCE;
3734

3835
private static final SpelExpressionParser PARSER = new SpelExpressionParser();
39-
private static final Logger LOG = LoggerFactory.getLogger(JpaQueryFactory.class);
4036

4137
/**
4238
* Creates a {@link RepositoryQuery} from the given {@link String} query.
@@ -48,15 +44,10 @@ enum JpaQueryFactory {
4844
* @param evaluationContextProvider
4945
* @return
5046
*/
51-
@Nullable
52-
AbstractJpaQuery fromMethodWithQueryString(JpaQueryMethod method, EntityManager em, @Nullable String queryString,
47+
AbstractJpaQuery fromMethodWithQueryString(JpaQueryMethod method, EntityManager em, String queryString,
5348
@Nullable String countQueryString,
5449
QueryMethodEvaluationContextProvider evaluationContextProvider) {
5550

56-
if (queryString == null) {
57-
return null;
58-
}
59-
6051
return method.isNativeQuery()
6152
? new NativeJpaQuery(method, em, queryString, countQueryString, evaluationContextProvider, PARSER)
6253
: new SimpleJpaQuery(method, em, queryString, countQueryString, evaluationContextProvider, PARSER);
@@ -69,13 +60,7 @@ AbstractJpaQuery fromMethodWithQueryString(JpaQueryMethod method, EntityManager
6960
* @param em must not be {@literal null}.
7061
* @return
7162
*/
72-
@Nullable
7363
public StoredProcedureJpaQuery fromProcedureAnnotation(JpaQueryMethod method, EntityManager em) {
74-
75-
if (!method.isProcedureQuery()) {
76-
return null;
77-
}
78-
7964
return new StoredProcedureJpaQuery(method, em);
8065
}
8166
}

src/main/java/org/springframework/data/jpa/repository/query/JpaQueryLookupStrategy.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -144,27 +144,24 @@ public DeclaredQueryLookupStrategy(EntityManager em, JpaQueryMethodFactory query
144144
@Override
145145
protected RepositoryQuery resolveQuery(JpaQueryMethod method, EntityManager em, NamedQueries namedQueries) {
146146

147-
String countQuery = getCountQuery(method, namedQueries, em);
147+
if (method.isProcedureQuery()) {
148+
return JpaQueryFactory.INSTANCE.fromProcedureAnnotation(method, em);
149+
}
148150

149151
if (StringUtils.hasText(method.getAnnotatedQuery())) {
150-
return JpaQueryFactory.INSTANCE.fromMethodWithQueryString(method, em, method.getAnnotatedQuery(), countQuery,
152+
return JpaQueryFactory.INSTANCE.fromMethodWithQueryString(method, em, method.getRequiredAnnotatedQuery(),
153+
getCountQuery(method, namedQueries, em),
151154
evaluationContextProvider);
152155
}
153156

154-
RepositoryQuery query = JpaQueryFactory.INSTANCE.fromProcedureAnnotation(method, em);
155-
156-
if (null != query) {
157-
return query;
158-
}
159-
160157
String name = method.getNamedQueryName();
161158
if (namedQueries.hasQuery(name)) {
162159
return JpaQueryFactory.INSTANCE.fromMethodWithQueryString(method, em, namedQueries.getQuery(name),
163-
countQuery,
160+
getCountQuery(method, namedQueries, em),
164161
evaluationContextProvider);
165162
}
166163

167-
query = NamedQuery.lookupFrom(method, em);
164+
RepositoryQuery query = NamedQuery.lookupFrom(method, em);
168165

169166
if (null != query) {
170167
return query;

0 commit comments

Comments
 (0)