diff --git a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryLookupStrategy.java b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryLookupStrategy.java index 62b71a9af..8757a7617 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryLookupStrategy.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryLookupStrategy.java @@ -16,6 +16,8 @@ package org.springframework.data.jpa.repository.query; +import java.lang.reflect.Method; + import javax.persistence.EntityManager; import org.springframework.data.jpa.repository.Query; @@ -38,14 +40,17 @@ public class JpaQueryLookupStrategy { * @author Oliver Gierke */ private static abstract class AbstractQueryLookupStrategy implements - QueryLookupStrategy { + QueryLookupStrategy { - private EntityManager em; + private final EntityManager em; + private final QueryExtractor provider; - public AbstractQueryLookupStrategy(EntityManager em) { + public AbstractQueryLookupStrategy(EntityManager em, + QueryExtractor extractor) { this.em = em; + this.provider = extractor; } @@ -56,9 +61,9 @@ public class JpaQueryLookupStrategy { * org.springframework.data.jpa.repository.query.QueryLookupStrategy * #resolveQuery(org.springframework.data.repository.query.QueryMethod) */ - public final RepositoryQuery resolveQuery(JpaQueryMethod method) { + public final RepositoryQuery resolveQuery(Method method) { - return resolveQuery(method, em); + return resolveQuery(new JpaQueryMethod(method, provider, em), em); } @@ -74,9 +79,10 @@ public class JpaQueryLookupStrategy { private static class CreateQueryLookupStrategy extends AbstractQueryLookupStrategy { - public CreateQueryLookupStrategy(EntityManager em) { + public CreateQueryLookupStrategy(EntityManager em, + QueryExtractor extractor) { - super(em); + super(em, extractor); } @@ -98,9 +104,10 @@ public class JpaQueryLookupStrategy { private static class DeclaredQueryLookupStrategy extends AbstractQueryLookupStrategy { - public DeclaredQueryLookupStrategy(EntityManager em) { + public DeclaredQueryLookupStrategy(EntityManager em, + QueryExtractor extractor) { - super(em); + super(em, extractor); } @@ -143,11 +150,12 @@ public class JpaQueryLookupStrategy { private final CreateQueryLookupStrategy createStrategy; - public CreateIfNotFoundQueryLookupStrategy(EntityManager em) { + public CreateIfNotFoundQueryLookupStrategy(EntityManager em, + QueryExtractor extractor) { - super(em); - this.strategy = new DeclaredQueryLookupStrategy(em); - this.createStrategy = new CreateQueryLookupStrategy(em); + super(em, extractor); + this.strategy = new DeclaredQueryLookupStrategy(em, extractor); + this.createStrategy = new CreateQueryLookupStrategy(em, extractor); } @@ -158,7 +166,7 @@ public class JpaQueryLookupStrategy { try { return strategy.resolveQuery(method, em); } catch (IllegalStateException e) { - return createStrategy.resolveQuery(method); + return createStrategy.resolveQuery(method, em); } } } @@ -172,20 +180,20 @@ public class JpaQueryLookupStrategy { * @param key * @return */ - public static QueryLookupStrategy create(EntityManager em, - Key key) { + public static QueryLookupStrategy create(EntityManager em, Key key, + QueryExtractor extractor) { if (key == null) { - return new CreateIfNotFoundQueryLookupStrategy(em); + return new CreateIfNotFoundQueryLookupStrategy(em, extractor); } switch (key) { case CREATE: - return new CreateQueryLookupStrategy(em); + return new CreateQueryLookupStrategy(em, extractor); case USE_DECLARED_QUERY: - return new DeclaredQueryLookupStrategy(em); + return new DeclaredQueryLookupStrategy(em, extractor); case CREATE_IF_NOT_FOUND: - return new CreateIfNotFoundQueryLookupStrategy(em); + return new CreateIfNotFoundQueryLookupStrategy(em, extractor); default: throw new IllegalArgumentException(String.format( "Unsupported query lookup strategy %!", key)); diff --git a/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactory.java b/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactory.java index fe99d530b..d8bb9125f 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactory.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactory.java @@ -1,12 +1,10 @@ package org.springframework.data.jpa.repository.support; import java.io.Serializable; -import java.lang.reflect.Method; import javax.persistence.EntityManager; import org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy; -import org.springframework.data.jpa.repository.query.JpaQueryMethod; import org.springframework.data.jpa.repository.query.QueryExtractor; import org.springframework.data.repository.query.QueryLookupStrategy; import org.springframework.data.repository.query.QueryLookupStrategy.Key; @@ -20,10 +18,10 @@ import org.springframework.util.Assert; * * @author Oliver Gierke */ -public class JpaRepositoryFactory extends - RepositoryFactorySupport { +public class JpaRepositoryFactory extends RepositoryFactorySupport { private final EntityManager entityManager; + private final QueryExtractor extractor; /** @@ -35,6 +33,7 @@ public class JpaRepositoryFactory extends Assert.notNull(entityManager); this.entityManager = entityManager; + this.extractor = PersistenceProvider.fromEntityManager(entityManager); } @@ -71,22 +70,6 @@ public class JpaRepositoryFactory extends } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.support.RepositoryFactorySupport# - * getQueryMethod(java.lang.reflect.Method) - */ - @Override - protected JpaQueryMethod getQueryMethod(Method method) { - - QueryExtractor extractor = - PersistenceProvider.fromEntityManager(entityManager); - return new JpaQueryMethod(method, extractor, entityManager); - } - - /* * (non-Javadoc) * @@ -111,8 +94,8 @@ public class JpaRepositoryFactory extends * (org.springframework.data.repository.query.QueryLookupStrategy.Key) */ @Override - protected QueryLookupStrategy getQueryLookupStrategy(Key key) { + protected QueryLookupStrategy getQueryLookupStrategy(Key key) { - return JpaQueryLookupStrategy.create(entityManager, key); + return JpaQueryLookupStrategy.create(entityManager, key, extractor); } } diff --git a/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactoryBean.java b/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactoryBean.java index 7e1655f16..8368bbad0 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactoryBean.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/JpaRepositoryFactoryBean.java @@ -71,7 +71,7 @@ public class JpaRepositoryFactoryBean> extends * #createRepositoryFactory() */ @Override - protected RepositoryFactorySupport createRepositoryFactory() { + protected RepositoryFactorySupport createRepositoryFactory() { return createRepositoryFactory(entityManager); } @@ -83,7 +83,7 @@ public class JpaRepositoryFactoryBean> extends * @param entityManager * @return */ - protected RepositoryFactorySupport createRepositoryFactory( + protected RepositoryFactorySupport createRepositoryFactory( EntityManager entityManager) { return new JpaRepositoryFactory(entityManager); diff --git a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java index 938c6c357..59ffc2e44 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java @@ -57,6 +57,7 @@ public class SimpleJpaRepository extends JpaRepositorySupport { private final EntityManager em; + private final PersistenceProvider provider; public SimpleJpaRepository(Class domainClass, EntityManager entityManager) { @@ -65,6 +66,7 @@ public class SimpleJpaRepository extends Assert.notNull(entityManager); this.em = entityManager; + this.provider = PersistenceProvider.fromEntityManager(entityManager); } @@ -76,8 +78,6 @@ public class SimpleJpaRepository extends private String getCountQueryString() { - PersistenceProvider provider = - PersistenceProvider.fromEntityManager(em); String countQuery = String.format(COUNT_QUERY_STRING, provider.getCountQueryPlaceholder(), "%s");