Polishing.

Make query factory methods non-nullable by moving conditionals to the lookup strategy.

See #2217
This commit is contained in:
Mark Paluch
2021-10-13 12:08:35 +02:00
parent d5cc7292ce
commit 66ca3ca4c8
2 changed files with 9 additions and 27 deletions

View File

@@ -17,9 +17,6 @@ package org.springframework.data.jpa.repository.query;
import javax.persistence.EntityManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.repository.query.QueryMethodEvaluationContextProvider;
import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.expression.spel.standard.SpelExpressionParser;
@@ -36,7 +33,6 @@ enum JpaQueryFactory {
INSTANCE;
private static final SpelExpressionParser PARSER = new SpelExpressionParser();
private static final Logger LOG = LoggerFactory.getLogger(JpaQueryFactory.class);
/**
* Creates a {@link RepositoryQuery} from the given {@link String} query.
@@ -48,15 +44,10 @@ enum JpaQueryFactory {
* @param evaluationContextProvider
* @return
*/
@Nullable
AbstractJpaQuery fromMethodWithQueryString(JpaQueryMethod method, EntityManager em, @Nullable String queryString,
AbstractJpaQuery fromMethodWithQueryString(JpaQueryMethod method, EntityManager em, String queryString,
@Nullable String countQueryString,
QueryMethodEvaluationContextProvider evaluationContextProvider) {
if (queryString == null) {
return null;
}
return method.isNativeQuery()
? new NativeJpaQuery(method, em, queryString, countQueryString, evaluationContextProvider, PARSER)
: new SimpleJpaQuery(method, em, queryString, countQueryString, evaluationContextProvider, PARSER);
@@ -69,13 +60,7 @@ enum JpaQueryFactory {
* @param em must not be {@literal null}.
* @return
*/
@Nullable
public StoredProcedureJpaQuery fromProcedureAnnotation(JpaQueryMethod method, EntityManager em) {
if (!method.isProcedureQuery()) {
return null;
}
return new StoredProcedureJpaQuery(method, em);
}
}

View File

@@ -144,27 +144,24 @@ public final class JpaQueryLookupStrategy {
@Override
protected RepositoryQuery resolveQuery(JpaQueryMethod method, EntityManager em, NamedQueries namedQueries) {
String countQuery = getCountQuery(method, namedQueries, em);
if (StringUtils.hasText(method.getAnnotatedQuery())) {
return JpaQueryFactory.INSTANCE.fromMethodWithQueryString(method, em, method.getAnnotatedQuery(), countQuery,
evaluationContextProvider);
if (method.isProcedureQuery()) {
return JpaQueryFactory.INSTANCE.fromProcedureAnnotation(method, em);
}
RepositoryQuery query = JpaQueryFactory.INSTANCE.fromProcedureAnnotation(method, em);
if (null != query) {
return query;
if (StringUtils.hasText(method.getAnnotatedQuery())) {
return JpaQueryFactory.INSTANCE.fromMethodWithQueryString(method, em, method.getRequiredAnnotatedQuery(),
getCountQuery(method, namedQueries, em),
evaluationContextProvider);
}
String name = method.getNamedQueryName();
if (namedQueries.hasQuery(name)) {
return JpaQueryFactory.INSTANCE.fromMethodWithQueryString(method, em, namedQueries.getQuery(name),
countQuery,
getCountQuery(method, namedQueries, em),
evaluationContextProvider);
}
query = NamedQuery.lookupFrom(method, em);
RepositoryQuery query = NamedQuery.lookupFrom(method, em);
if (null != query) {
return query;