Skip to content

Commit 1a00e2f

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

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
@@ -149,7 +149,9 @@ public DeclaredQueryLookupStrategy(EntityManager em, JpaQueryMethodFactory query
149149
@Override
150150
protected RepositoryQuery resolveQuery(JpaQueryMethod method, EntityManager em, NamedQueries namedQueries) {
151151

152-
String countQuery = getCountQuery(method, namedQueries, em);
152+
if (method.isProcedureQuery()) {
153+
return JpaQueryFactory.INSTANCE.fromProcedureAnnotation(method, em);
154+
}
153155

154156
if (StringUtils.hasText(method.getAnnotatedQuery())) {
155157

@@ -158,23 +160,18 @@ protected RepositoryQuery resolveQuery(JpaQueryMethod method, EntityManager em,
158160
"Query method %s is annotated with both, a query and a query name. Using the declared query.", method));
159161
}
160162

161-
return JpaQueryFactory.INSTANCE.fromMethodWithQueryString(method, em, method.getAnnotatedQuery(), countQuery,
163+
return JpaQueryFactory.INSTANCE.fromMethodWithQueryString(method, em, method.getRequiredAnnotatedQuery(),
164+
getCountQuery(method, namedQueries, em),
162165
evaluationContextProvider);
163166
}
164167

165-
RepositoryQuery query = JpaQueryFactory.INSTANCE.fromProcedureAnnotation(method, em);
166-
167-
if (null != query) {
168-
return query;
169-
}
170-
171168
String name = method.getNamedQueryName();
172169
if (namedQueries.hasQuery(name)) {
173-
return JpaQueryFactory.INSTANCE.fromMethodWithQueryString(method, em, namedQueries.getQuery(name), countQuery,
170+
return JpaQueryFactory.INSTANCE.fromMethodWithQueryString(method, em, namedQueries.getQuery(name), getCountQuery(method, namedQueries, em),
174171
evaluationContextProvider);
175172
}
176173

177-
query = NamedQuery.lookupFrom(method, em);
174+
RepositoryQuery query = NamedQuery.lookupFrom(method, em);
178175

179176
if (null != query) {
180177
return query;

0 commit comments

Comments
 (0)