From d0111d8118de089bd65a76a522187168308463b8 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 28 Nov 2012 17:18:25 +0100 Subject: [PATCH] DATACMNS-256 - Exposing getPersistentEntity() on Repositories. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extended RepositoryFactoryInformation to expose the PersistentEntity for the domain type the repository factory is eventually creating the repository for. The default implementation in RepositoryFactoryBeanSupport exposes a protected setMappingContext(…) that is later used to lookup the PersistentEntity. --- .../support/RepositoryFactoryBeanSupport.java | 28 +++++++++++++++++++ .../support/RepositoryFactoryInformation.java | 10 +++++++ .../data/repository/support/Repositories.java | 16 +++++++++++ .../support/RepositoriesUnitTests.java | 21 +++++++++++++- 4 files changed, 74 insertions(+), 1 deletion(-) diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/RepositoryFactoryBeanSupport.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/RepositoryFactoryBeanSupport.java index e97df11d8..302510a90 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/RepositoryFactoryBeanSupport.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/RepositoryFactoryBeanSupport.java @@ -21,6 +21,8 @@ import java.util.List; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Required; +import org.springframework.data.mapping.PersistentEntity; +import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.repository.Repository; import org.springframework.data.repository.core.EntityInformation; import org.springframework.data.repository.core.NamedQueries; @@ -47,6 +49,7 @@ public abstract class RepositoryFactoryBeanSupport, private Class repositoryInterface; private Object customImplementation; private NamedQueries namedQueries; + private MappingContext mappingContext; /** * Setter to inject the repository interface to implement. @@ -89,6 +92,16 @@ public abstract class RepositoryFactoryBeanSupport, this.namedQueries = namedQueries; } + /** + * Configures the {@link MappingContext} to be used to lookup {@link PersistentEntity} instances for + * {@link #getPersistentEntity()}. + * + * @param mappingContext + */ + protected void setMappingContext(MappingContext mappingContext) { + this.mappingContext = mappingContext; + } + /* * (non-Javadoc) * @see org.springframework.data.repository.core.support.RepositoryFactoryInformation#getEntityInformation() @@ -105,11 +118,26 @@ public abstract class RepositoryFactoryBeanSupport, * @see org.springframework.data.repository.core.support.RepositoryFactoryInformation#getRepositoryInformation() */ public RepositoryInformation getRepositoryInformation() { + RepositoryMetadata metadata = factory.getRepositoryMetadata(repositoryInterface); return this.factory.getRepositoryInformation(metadata, customImplementation == null ? null : customImplementation.getClass()); } + /* + * (non-Javadoc) + * @see org.springframework.data.repository.core.support.RepositoryFactoryInformation#getPersistentEntity() + */ + public PersistentEntity getPersistentEntity() { + + if (mappingContext == null) { + return null; + } + + RepositoryMetadata metadata = factory.getRepositoryMetadata(repositoryInterface); + return mappingContext.getPersistentEntity(metadata.getDomainType()); + } + /* (non-Javadoc) * @see org.springframework.data.repository.core.support.RepositoryFactoryInformation#getQueryMethods() */ diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/RepositoryFactoryInformation.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/RepositoryFactoryInformation.java index 4ebbeb007..7f35190c4 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/RepositoryFactoryInformation.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/core/support/RepositoryFactoryInformation.java @@ -18,6 +18,8 @@ package org.springframework.data.repository.core.support; import java.io.Serializable; import java.util.List; +import org.springframework.data.mapping.PersistentEntity; +import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.repository.core.EntityInformation; import org.springframework.data.repository.core.RepositoryInformation; import org.springframework.data.repository.query.QueryMethod; @@ -45,6 +47,14 @@ public interface RepositoryFactoryInformation { */ RepositoryInformation getRepositoryInformation(); + /** + * Returns the {@link PersistentEntity} managed by the underlying repository. Can be {@literal null} in case the + * underlying persistence mechanism does not expose a {@link MappingContext}. + * + * @return + */ + PersistentEntity getPersistentEntity(); + /** * Returns all {@link QueryMethod}s declared for that repository. * diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/Repositories.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/Repositories.java index 264007e97..f82ead1ae 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/Repositories.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/Repositories.java @@ -25,6 +25,8 @@ import java.util.Map; import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.ListableBeanFactory; +import org.springframework.data.mapping.PersistentEntity; +import org.springframework.data.mapping.context.MappingContext; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.core.EntityInformation; import org.springframework.data.repository.core.RepositoryInformation; @@ -128,6 +130,20 @@ public class Repositories implements Iterable> { return information == null ? null : information.getRepositoryInformation(); } + /** + * Returns the {@link PersistentEntity} for the given domain class. Might return {@literal null} in case the module + * storing the given domain class does not support the mapping subsystem. + * + * @param domainClass must not be {@literal null}. + * @return the {@link PersistentEntity} for the given domain class or {@literal null} if no repository is registered + * for the domain class or the repository is not backed by a {@link MappingContext} implementation. + */ + public PersistentEntity getPersistentEntity(Class domainClass) { + + RepositoryFactoryInformation information = getRepoInfoFor(domainClass); + return information == null ? null : information.getPersistentEntity(); + } + /** * Returns the {@link QueryMethod}s contained in the repository managing the given domain class. * diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/RepositoriesUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/RepositoriesUnitTests.java index 1d45ff16b..0060ae281 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/RepositoriesUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/RepositoriesUnitTests.java @@ -33,6 +33,8 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.springframework.context.ApplicationContext; +import org.springframework.data.mapping.MappingMetadataTests.SampleMappingContext; +import org.springframework.data.mapping.PersistentEntity; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.Repository; import org.springframework.data.repository.core.EntityInformation; @@ -93,6 +95,17 @@ public class RepositoriesUnitTests { new Repositories(null); } + /** + * @see DATACMNS-256 + */ + @Test + public void exposesPersistentEntityForDomainTypes() { + + Repositories repositories = new Repositories(context); + assertThat(repositories.getPersistentEntity(Person.class), is(notNullValue())); + assertThat(repositories.getPersistentEntity(Address.class), is(nullValue())); + } + class Person { } @@ -112,20 +125,26 @@ public class RepositoriesUnitTests { static class SampleRepoFactoryInformation implements RepositoryFactoryInformation { private final RepositoryMetadata repositoryMetadata; + private final SampleMappingContext mappingContext; public SampleRepoFactoryInformation(Class repositoryInterface) { this.repositoryMetadata = new DefaultRepositoryMetadata(repositoryInterface); + this.mappingContext = new SampleMappingContext(); } @SuppressWarnings({ "unchecked", "rawtypes" }) public EntityInformation getEntityInformation() { - return (EntityInformation) new DummyEntityInformation(repositoryMetadata.getDomainType()); + return new DummyEntityInformation(repositoryMetadata.getDomainType()); } public RepositoryInformation getRepositoryInformation() { return new DummyRepositoryInformation(repositoryMetadata.getRepositoryInterface()); } + public PersistentEntity getPersistentEntity() { + return mappingContext.getPersistentEntity(repositoryMetadata.getDomainType()); + } + public List getQueryMethods() { return Collections.emptyList(); }