Reflect changes made to QueryLookupStrategy.

Some minor improvements in terms of reducing the number of times we lookup the actual PersistenceProvider by reflection.
This commit is contained in:
Oliver Gierke
2010-11-30 12:42:51 +01:00
parent 8b76c04afd
commit d3254dc2d1
4 changed files with 37 additions and 46 deletions

View File

@@ -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<JpaQueryMethod> {
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<JpaQueryMethod> 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));

View File

@@ -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<JpaQueryMethod> {
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<JpaQueryMethod> getQueryLookupStrategy(Key key) {
protected QueryLookupStrategy getQueryLookupStrategy(Key key) {
return JpaQueryLookupStrategy.create(entityManager, key);
return JpaQueryLookupStrategy.create(entityManager, key, extractor);
}
}

View File

@@ -71,7 +71,7 @@ public class JpaRepositoryFactoryBean<T extends JpaRepository<?, ?>> extends
* #createRepositoryFactory()
*/
@Override
protected RepositoryFactorySupport<?> createRepositoryFactory() {
protected RepositoryFactorySupport createRepositoryFactory() {
return createRepositoryFactory(entityManager);
}
@@ -83,7 +83,7 @@ public class JpaRepositoryFactoryBean<T extends JpaRepository<?, ?>> extends
* @param entityManager
* @return
*/
protected RepositoryFactorySupport<?> createRepositoryFactory(
protected RepositoryFactorySupport createRepositoryFactory(
EntityManager entityManager) {
return new JpaRepositoryFactory(entityManager);

View File

@@ -57,6 +57,7 @@ public class SimpleJpaRepository<T, ID extends Serializable> extends
JpaRepositorySupport<T, ID> {
private final EntityManager em;
private final PersistenceProvider provider;
public SimpleJpaRepository(Class<T> domainClass, EntityManager entityManager) {
@@ -65,6 +66,7 @@ public class SimpleJpaRepository<T, ID extends Serializable> extends
Assert.notNull(entityManager);
this.em = entityManager;
this.provider = PersistenceProvider.fromEntityManager(entityManager);
}
@@ -76,8 +78,6 @@ public class SimpleJpaRepository<T, ID extends Serializable> extends
private String getCountQueryString() {
PersistenceProvider provider =
PersistenceProvider.fromEntityManager(em);
String countQuery =
String.format(COUNT_QUERY_STRING,
provider.getCountQueryPlaceholder(), "%s");