DATAJPA-1230 - Polishing.

Introduced JpaRepositoryImplementation interface to combine JpaRepository, JpaSpecificationExecutor and a callback to set CrudMethodMetadata on the implementation instance. Refactored JpaRepositoryFactory to only rely on that interface and avoid references to SimpleJpaRepository.
This commit is contained in:
Oliver Gierke
2017-11-29 15:03:05 +01:00
parent 31ddaa979e
commit f78037410d
3 changed files with 24 additions and 20 deletions

View File

@@ -86,13 +86,10 @@ public class JpaRepositoryFactory extends RepositoryFactorySupport {
* @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getTargetRepository(org.springframework.data.repository.core.RepositoryMetadata)
*/
@Override
protected Object getTargetRepository(RepositoryInformation information) {
protected final JpaRepositoryImplementation<?, ?> getTargetRepository(RepositoryInformation information) {
Object repository = getTargetRepository(information, entityManager);
if (repository instanceof RepositoryMethodMetadataAware) {
((RepositoryMethodMetadataAware) repository)
.setRepositoryMethodMetadata(crudMethodMetadataPostProcessor.getCrudMethodMetadata());
}
JpaRepositoryImplementation<?, ?> repository = getTargetRepository(information, entityManager);
repository.setRepositoryMethodMetadata(crudMethodMetadataPostProcessor.getCrudMethodMetadata());
return repository;
}
@@ -100,16 +97,19 @@ public class JpaRepositoryFactory extends RepositoryFactorySupport {
/**
* Callback to create a {@link JpaRepository} instance with the given {@link EntityManager}
*
* @param <T>
* @param <ID>
* @param entityManager
* @param information will never be {@literal null}.
* @param entityManager will never be {@literal null}.
* @return
*/
protected Object getTargetRepository(RepositoryInformation information, EntityManager entityManager) {
protected JpaRepositoryImplementation<?, ?> getTargetRepository(RepositoryInformation information,
EntityManager entityManager) {
JpaEntityInformation<?, Serializable> entityInformation = getEntityInformation(information.getDomainType());
Object repository = getTargetRepositoryViaReflection(information, entityInformation, entityManager);
return getTargetRepositoryViaReflection(information, entityInformation, entityManager);
Assert.isInstanceOf(JpaRepositoryImplementation.class, repository);
return (JpaRepositoryImplementation<?, ?>) repository;
}
/*

View File

@@ -15,16 +15,23 @@
*/
package org.springframework.data.jpa.repository.support;
import org.springframework.data.jpa.repository.support.CrudMethodMetadata;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.NoRepositoryBean;
/**
* implemented by {@link org.springframework.data.jpa.repository.JpaRepository} implementations requiring
* {@link CrudMethodMetadata}
* SPI interface to be implemented by {@link JpaRepository} implementations.
*
* @author Oliver Gierke
* @author Stefan Fussenegger
*/
public interface RepositoryMethodMetadataAware {
@NoRepositoryBean
public interface JpaRepositoryImplementation<T, ID> extends JpaRepository<T, ID>, JpaSpecificationExecutor<T> {
/**
* Configures the {@link CrudMethodMetadata} to be used with the repository.
*
* @param crudMethodMetadata must not be {@literal null}.
*/
void setRepositoryMethodMetadata(CrudMethodMetadata crudMethodMetadata);
}

View File

@@ -48,8 +48,6 @@ import org.springframework.data.jpa.convert.QueryByExamplePredicateBuilder;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.provider.PersistenceProvider;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.query.QueryUtils;
import org.springframework.data.jpa.repository.support.QueryHints.NoHints;
import org.springframework.data.repository.support.PageableExecutionUtils;
@@ -73,8 +71,7 @@ import org.springframework.util.Assert;
*/
@Repository
@Transactional(readOnly = true)
public class SimpleJpaRepository<T, ID>
implements JpaRepository<T, ID>, JpaSpecificationExecutor<T>, RepositoryMethodMetadataAware {
public class SimpleJpaRepository<T, ID> implements JpaRepositoryImplementation<T, ID> {
private static final String ID_MUST_NOT_BE_NULL = "The given id must not be null!";