DATACMNS-256 - Exposing getPersistentEntity() on Repositories.

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.
This commit is contained in:
Oliver Gierke
2012-11-28 17:18:25 +01:00
parent d5e0517517
commit d0111d8118
4 changed files with 74 additions and 1 deletions

View File

@@ -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<T extends Repository<S, ID>,
private Class<? extends T> 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<T extends Repository<S, ID>,
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<T extends Repository<S, ID>,
* @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()
*/

View File

@@ -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<T, ID extends Serializable> {
*/
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.
*

View File

@@ -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<Class<?>> {
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<Object, Serializable> information = getRepoInfoFor(domainClass);
return information == null ? null : information.getPersistentEntity();
}
/**
* Returns the {@link QueryMethod}s contained in the repository managing the given domain class.
*

View File

@@ -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<T, S extends Serializable> implements RepositoryFactoryInformation<T, S> {
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<T, S> 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<QueryMethod> getQueryMethods() {
return Collections.emptyList();
}