DATAJPA-1663 - Polishing.
Removed QueryExtractor from JpaQueryLookupStrategy entirely as it's not needed anymore. Slightly adapted method signatures for consistent parameter order. Original pull request: #408.
This commit is contained in:
@@ -19,7 +19,6 @@ import java.lang.reflect.Method;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
import org.springframework.data.jpa.provider.QueryExtractor;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.projection.ProjectionFactory;
|
||||
import org.springframework.data.repository.core.NamedQueries;
|
||||
@@ -55,21 +54,20 @@ public final class JpaQueryLookupStrategy {
|
||||
private abstract static class AbstractQueryLookupStrategy implements QueryLookupStrategy {
|
||||
|
||||
private final EntityManager em;
|
||||
private final QueryExtractor provider;
|
||||
private final JpaQueryMethodFactory queryMethodFactory;
|
||||
|
||||
/**
|
||||
* Creates a new {@link AbstractQueryLookupStrategy}.
|
||||
*
|
||||
* @param em
|
||||
* @param extractor
|
||||
* @param queryMethodFactory
|
||||
* @param em must not be {@literal null}.
|
||||
* @param queryMethodFactory must not be {@literal null}.
|
||||
*/
|
||||
public AbstractQueryLookupStrategy(EntityManager em, QueryExtractor extractor,
|
||||
JpaQueryMethodFactory queryMethodFactory) {
|
||||
public AbstractQueryLookupStrategy(EntityManager em, JpaQueryMethodFactory queryMethodFactory) {
|
||||
|
||||
Assert.notNull(em, "EntityManager must not be null!");
|
||||
Assert.notNull(queryMethodFactory, "JpaQueryMethodFactory must not be null!");
|
||||
|
||||
this.em = em;
|
||||
this.provider = extractor;
|
||||
this.queryMethodFactory = queryMethodFactory;
|
||||
}
|
||||
|
||||
@@ -96,10 +94,10 @@ public final class JpaQueryLookupStrategy {
|
||||
|
||||
private final EscapeCharacter escape;
|
||||
|
||||
public CreateQueryLookupStrategy(EntityManager em, QueryExtractor extractor, EscapeCharacter escape,
|
||||
JpaQueryMethodFactory queryMethodFactory) {
|
||||
public CreateQueryLookupStrategy(EntityManager em, JpaQueryMethodFactory queryMethodFactory,
|
||||
EscapeCharacter escape) {
|
||||
|
||||
super(em, extractor, queryMethodFactory);
|
||||
super(em, queryMethodFactory);
|
||||
|
||||
this.escape = escape;
|
||||
}
|
||||
@@ -130,10 +128,10 @@ public final class JpaQueryLookupStrategy {
|
||||
* @param queryMethodFactory
|
||||
* @param evaluationContextProvider
|
||||
*/
|
||||
public DeclaredQueryLookupStrategy(EntityManager em, QueryExtractor extractor,
|
||||
JpaQueryMethodFactory queryMethodFactory, QueryMethodEvaluationContextProvider evaluationContextProvider) {
|
||||
public DeclaredQueryLookupStrategy(EntityManager em, JpaQueryMethodFactory queryMethodFactory,
|
||||
QueryMethodEvaluationContextProvider evaluationContextProvider) {
|
||||
|
||||
super(em, extractor, queryMethodFactory);
|
||||
super(em, queryMethodFactory);
|
||||
|
||||
this.evaluationContextProvider = evaluationContextProvider;
|
||||
}
|
||||
@@ -190,17 +188,18 @@ public final class JpaQueryLookupStrategy {
|
||||
/**
|
||||
* Creates a new {@link CreateIfNotFoundQueryLookupStrategy}.
|
||||
*
|
||||
* @param em
|
||||
* @param extractor
|
||||
* @param queryMethodFactory
|
||||
* @param createStrategy
|
||||
* @param lookupStrategy
|
||||
* @param em must not be {@literal null}.
|
||||
* @param queryMethodFactory must not be {@literal null}.
|
||||
* @param createStrategy must not be {@literal null}.
|
||||
* @param lookupStrategy must not be {@literal null}.
|
||||
*/
|
||||
public CreateIfNotFoundQueryLookupStrategy(EntityManager em, QueryExtractor extractor,
|
||||
JpaQueryMethodFactory queryMethodFactory, CreateQueryLookupStrategy createStrategy,
|
||||
DeclaredQueryLookupStrategy lookupStrategy) {
|
||||
public CreateIfNotFoundQueryLookupStrategy(EntityManager em, JpaQueryMethodFactory queryMethodFactory,
|
||||
CreateQueryLookupStrategy createStrategy, DeclaredQueryLookupStrategy lookupStrategy) {
|
||||
|
||||
super(em, extractor, queryMethodFactory);
|
||||
super(em, queryMethodFactory);
|
||||
|
||||
Assert.notNull(createStrategy, "CreateQueryLookupStrategy must not be null!");
|
||||
Assert.notNull(lookupStrategy, "DeclaredQueryLookupStrategy must not be null!");
|
||||
|
||||
this.createStrategy = createStrategy;
|
||||
this.lookupStrategy = lookupStrategy;
|
||||
@@ -225,30 +224,28 @@ public final class JpaQueryLookupStrategy {
|
||||
* Creates a {@link QueryLookupStrategy} for the given {@link EntityManager} and {@link Key}.
|
||||
*
|
||||
* @param em must not be {@literal null}.
|
||||
* @param key may be {@literal null}.
|
||||
* @param extractor must not be {@literal null}.
|
||||
* @param queryMethodFactory must not be {@literal null}.
|
||||
* @param key may be {@literal null}.
|
||||
* @param evaluationContextProvider must not be {@literal null}.
|
||||
* @param escape
|
||||
* @param extractor must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public static QueryLookupStrategy create(EntityManager em, @Nullable Key key, QueryExtractor extractor,
|
||||
JpaQueryMethodFactory queryMethodFactory, QueryMethodEvaluationContextProvider evaluationContextProvider,
|
||||
EscapeCharacter escape) {
|
||||
public static QueryLookupStrategy create(EntityManager em, JpaQueryMethodFactory queryMethodFactory,
|
||||
@Nullable Key key, QueryMethodEvaluationContextProvider evaluationContextProvider, EscapeCharacter escape) {
|
||||
|
||||
Assert.notNull(em, "EntityManager must not be null!");
|
||||
Assert.notNull(extractor, "QueryExtractor must not be null!");
|
||||
Assert.notNull(evaluationContextProvider, "EvaluationContextProvider must not be null!");
|
||||
|
||||
switch (key != null ? key : Key.CREATE_IF_NOT_FOUND) {
|
||||
case CREATE:
|
||||
return new CreateQueryLookupStrategy(em, extractor, escape, queryMethodFactory);
|
||||
return new CreateQueryLookupStrategy(em, queryMethodFactory, escape);
|
||||
case USE_DECLARED_QUERY:
|
||||
return new DeclaredQueryLookupStrategy(em, extractor, queryMethodFactory, evaluationContextProvider);
|
||||
return new DeclaredQueryLookupStrategy(em, queryMethodFactory, evaluationContextProvider);
|
||||
case CREATE_IF_NOT_FOUND:
|
||||
return new CreateIfNotFoundQueryLookupStrategy(em, extractor, queryMethodFactory,
|
||||
new CreateQueryLookupStrategy(em, extractor, escape, queryMethodFactory),
|
||||
new DeclaredQueryLookupStrategy(em, extractor, queryMethodFactory, evaluationContextProvider));
|
||||
return new CreateIfNotFoundQueryLookupStrategy(em, queryMethodFactory,
|
||||
new CreateQueryLookupStrategy(em, queryMethodFactory, escape),
|
||||
new DeclaredQueryLookupStrategy(em, queryMethodFactory, evaluationContextProvider));
|
||||
default:
|
||||
throw new IllegalArgumentException(String.format("Unsupported query lookup strategy %s!", key));
|
||||
}
|
||||
|
||||
@@ -425,7 +425,9 @@ public class JpaMetamodelEntityInformation<T, ID> extends JpaEntityInformationSu
|
||||
|
||||
ManagedType<?> managedType = this.metamodel.managedType(userClass);
|
||||
|
||||
Assert.state(managedType != null, "ManagedType must not be null. We checked that it exists before.");
|
||||
if (managedType == null) {
|
||||
throw new IllegalStateException("ManagedType must not be null. We checked that it exists before.");
|
||||
}
|
||||
|
||||
return managedType.getPersistenceType() == PersistenceType.ENTITY;
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.jpa.repository.support;
|
||||
|
||||
import static org.springframework.data.querydsl.QuerydslUtils.QUERY_DSL_PRESENT;
|
||||
import static org.springframework.data.querydsl.QuerydslUtils.*;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@@ -140,8 +140,9 @@ public class JpaRepositoryFactory extends RepositoryFactorySupport {
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures the {@link JpaQueryMethodFactory} to be used. Defaults to {@link JpaQueryMethod.DefaultJpaQueryMethodFactory#INSTANCE}.
|
||||
*
|
||||
* Configures the {@link JpaQueryMethodFactory} to be used. Defaults to
|
||||
* {@link JpaQueryMethod.DefaultJpaQueryMethodFactory#INSTANCE}.
|
||||
*
|
||||
* @param queryMethodFactory must not be {@literal null}.
|
||||
*/
|
||||
public void setQueryMethodFactory(JpaQueryMethodFactory queryMethodFactory) {
|
||||
@@ -213,9 +214,8 @@ public class JpaRepositoryFactory extends RepositoryFactorySupport {
|
||||
@Override
|
||||
protected Optional<QueryLookupStrategy> getQueryLookupStrategy(@Nullable Key key,
|
||||
QueryMethodEvaluationContextProvider evaluationContextProvider) {
|
||||
return Optional
|
||||
.of(
|
||||
JpaQueryLookupStrategy.create(entityManager, key, extractor, queryMethodFactory, evaluationContextProvider, escapeCharacter));
|
||||
return Optional.of(JpaQueryLookupStrategy.create(entityManager, queryMethodFactory, key, evaluationContextProvider,
|
||||
escapeCharacter));
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -79,8 +79,8 @@ public class JpaQueryLookupStrategyUnitTests {
|
||||
@Test // DATAJPA-226
|
||||
public void invalidAnnotatedQueryCausesException() throws Exception {
|
||||
|
||||
QueryLookupStrategy strategy = JpaQueryLookupStrategy.create(em, Key.CREATE_IF_NOT_FOUND, extractor,
|
||||
queryMethodFactory, EVALUATION_CONTEXT_PROVIDER, EscapeCharacter.DEFAULT);
|
||||
QueryLookupStrategy strategy = JpaQueryLookupStrategy.create(em, queryMethodFactory, Key.CREATE_IF_NOT_FOUND,
|
||||
EVALUATION_CONTEXT_PROVIDER, EscapeCharacter.DEFAULT);
|
||||
Method method = UserRepository.class.getMethod("findByFoo", String.class);
|
||||
RepositoryMetadata metadata = new DefaultRepositoryMetadata(UserRepository.class);
|
||||
|
||||
@@ -95,8 +95,8 @@ public class JpaQueryLookupStrategyUnitTests {
|
||||
@Test // DATAJPA-554
|
||||
public void sholdThrowMorePreciseExceptionIfTryingToUsePaginationInNativeQueries() throws Exception {
|
||||
|
||||
QueryLookupStrategy strategy = JpaQueryLookupStrategy.create(em, Key.CREATE_IF_NOT_FOUND, extractor,
|
||||
queryMethodFactory, EVALUATION_CONTEXT_PROVIDER, EscapeCharacter.DEFAULT);
|
||||
QueryLookupStrategy strategy = JpaQueryLookupStrategy.create(em, queryMethodFactory, Key.CREATE_IF_NOT_FOUND,
|
||||
EVALUATION_CONTEXT_PROVIDER, EscapeCharacter.DEFAULT);
|
||||
Method method = UserRepository.class.getMethod("findByInvalidNativeQuery", String.class, Sort.class);
|
||||
RepositoryMetadata metadata = new DefaultRepositoryMetadata(UserRepository.class);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user