DATACMNS-673 - Enhanced RepositoryMetadata to allow returning alternative domain types.

In case a RepositoryMetadata implementation alters the domain type the repository is actually handling, we still have to be able to find the repository based on the type originally declared in the repository.

The newly introduced getAlternativeDomainTypes() is now used by Repositories to register a repository for all types returned by that method, too, so that this reverse lookup still works.
This commit is contained in:
Oliver Gierke
2015-03-31 16:30:39 +02:00
parent 5d6fd02ce1
commit 1c175ef59b
6 changed files with 143 additions and 12 deletions

View File

@@ -29,7 +29,11 @@ public final class DummyRepositoryInformation implements RepositoryInformation {
private final RepositoryMetadata metadata;
public DummyRepositoryInformation(Class<?> repositoryInterface) {
this.metadata = new DefaultRepositoryMetadata(repositoryInterface);
this(new DefaultRepositoryMetadata(repositoryInterface));
}
public DummyRepositoryInformation(RepositoryMetadata metadata) {
this.metadata = metadata;
}
public Class<? extends Serializable> getIdType() {
@@ -84,4 +88,9 @@ public final class DummyRepositoryInformation implements RepositoryInformation {
public boolean isPagingRepository() {
return false;
}
@Override
public Set<Class<?>> getAlternativeDomainTypes() {
return metadata.getAlternativeDomainTypes();
}
}

View File

@@ -23,6 +23,7 @@ import static org.junit.Assert.*;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.junit.Before;
import org.junit.Test;
@@ -45,6 +46,7 @@ import org.springframework.data.repository.core.support.DummyRepositoryFactoryBe
import org.springframework.data.repository.core.support.DummyRepositoryInformation;
import org.springframework.data.repository.core.support.RepositoryFactoryInformation;
import org.springframework.data.repository.query.QueryMethod;
import org.springframework.util.ClassUtils;
/**
* Unit tests for {@link Repositories}.
@@ -116,6 +118,27 @@ public class RepositoriesUnitTests {
assertThat(new Repositories(context).getPersistentEntity(AdvancedAddress.class), is(notNullValue()));
}
/**
* @see DATACMNS-673
*/
@Test
public void discoversRepositoryForAlternativeDomainType() {
RepositoryMetadata metadata = new CustomRepositoryMetadata(SampleRepository.class);
RepositoryFactoryInformation<?, ?> information = new SampleRepoFactoryInformation<Object, Serializable>(metadata);
GenericApplicationContext context = new GenericApplicationContext();
context.getBeanFactory().registerSingleton("foo", information);
context.refresh();
Repositories repositories = new Repositories(context);
assertThat(repositories.getRepositoryFor(Sample.class), is(notNullValue()));
assertThat(repositories.getRepositoryFor(SampleEntity.class), is(notNullValue()));
context.close();
}
class Person {}
class Address {}
@@ -132,7 +155,11 @@ public class RepositoriesUnitTests {
private final SampleMappingContext mappingContext;
public SampleRepoFactoryInformation(Class<?> repositoryInterface) {
this.repositoryMetadata = new DefaultRepositoryMetadata(repositoryInterface);
this(new DefaultRepositoryMetadata(repositoryInterface));
}
public SampleRepoFactoryInformation(RepositoryMetadata metadata) {
this.repositoryMetadata = metadata;
this.mappingContext = new SampleMappingContext();
}
@@ -142,7 +169,7 @@ public class RepositoriesUnitTests {
}
public RepositoryInformation getRepositoryInformation() {
return new DummyRepositoryInformation(repositoryMetadata.getRepositoryInterface());
return new DummyRepositoryInformation(repositoryMetadata);
}
public PersistentEntity<?, ?> getPersistentEntity() {
@@ -153,4 +180,49 @@ public class RepositoriesUnitTests {
return Collections.emptyList();
}
}
static class CustomRepositoryMetadata extends DefaultRepositoryMetadata {
private final Class<?> domainType;
/**
* @param repositoryInterface
*/
public CustomRepositoryMetadata(Class<?> repositoryInterface) {
super(repositoryInterface);
String domainType = super.getDomainType().getName().concat("Entity");
try {
this.domainType = ClassUtils.forName(domainType, CustomRepositoryMetadata.class.getClassLoader());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.core.support.DefaultRepositoryMetadata#getDomainType()
*/
@Override
public Class<?> getDomainType() {
return this.domainType;
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.core.support.AbstractRepositoryMetadata#getAlternativeDomainTypes()
*/
@Override
public Set<Class<?>> getAlternativeDomainTypes() {
return Collections.<Class<?>> singleton(super.getDomainType());
}
}
interface Sample {}
static class SampleEntity implements Sample {}
interface SampleRepository extends Repository<Sample, Long> {}
}