Polishing.
Make query factory methods non-nullable by moving conditionals to the lookup strategy. See #2217
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,7 +149,9 @@ public final class JpaQueryLookupStrategy {
|
||||
@Override
|
||||
protected RepositoryQuery resolveQuery(JpaQueryMethod method, EntityManager em, NamedQueries namedQueries) {
|
||||
|
||||
String countQuery = getCountQuery(method, namedQueries, em);
|
||||
if (method.isProcedureQuery()) {
|
||||
return JpaQueryFactory.INSTANCE.fromProcedureAnnotation(method, em);
|
||||
}
|
||||
|
||||
if (StringUtils.hasText(method.getAnnotatedQuery())) {
|
||||
|
||||
@@ -158,23 +160,18 @@ public final class JpaQueryLookupStrategy {
|
||||
"Query method %s is annotated with both, a query and a query name. Using the declared query.", method));
|
||||
}
|
||||
|
||||
return JpaQueryFactory.INSTANCE.fromMethodWithQueryString(method, em, method.getAnnotatedQuery(), countQuery,
|
||||
return JpaQueryFactory.INSTANCE.fromMethodWithQueryString(method, em, method.getRequiredAnnotatedQuery(),
|
||||
getCountQuery(method, namedQueries, em),
|
||||
evaluationContextProvider);
|
||||
}
|
||||
|
||||
RepositoryQuery query = JpaQueryFactory.INSTANCE.fromProcedureAnnotation(method, em);
|
||||
|
||||
if (null != query) {
|
||||
return query;
|
||||
}
|
||||
|
||||
String name = method.getNamedQueryName();
|
||||
if (namedQueries.hasQuery(name)) {
|
||||
return JpaQueryFactory.INSTANCE.fromMethodWithQueryString(method, em, namedQueries.getQuery(name), countQuery,
|
||||
return JpaQueryFactory.INSTANCE.fromMethodWithQueryString(method, em, namedQueries.getQuery(name), getCountQuery(method, namedQueries, em),
|
||||
evaluationContextProvider);
|
||||
}
|
||||
|
||||
query = NamedQuery.lookupFrom(method, em);
|
||||
RepositoryQuery query = NamedQuery.lookupFrom(method, em);
|
||||
|
||||
if (null != query) {
|
||||
return query;
|
||||
|
||||
Reference in New Issue
Block a user