Repositories now allows lookup of parent repositories for sub-types.
When inheritance is used for aggregates, lookups of the repository for a Child aggregate instance have so far failed to return a repository registered for the Parent. Client code had to consider that scenario explicitly. This commit introduces an additional lookup step in case we cannot find a repository for an aggregate type immediately. In this case, we then check for assignability of any of the known aggregate types we have registered repositories for and the type requested. I.e. for a request for the repository of Child, a repository, explicitly registered to manage Child instances would still be used. In the sole presence of a repository managing Parent instances, that would be returned for the request for Child, too. Original pull request: #2406.
This commit is contained in:
committed by
Mark Paluch
parent
394187a03b
commit
27eac58691
@@ -29,7 +29,6 @@ import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.mockito.junit.jupiter.MockitoSettings;
|
||||
import org.mockito.quality.Strictness;
|
||||
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
@@ -206,6 +205,47 @@ class RepositoriesUnitTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test // GH-2406
|
||||
void exposesParentRepositoryForChildIfOnlyParentRepositoryIsRegistered() {
|
||||
|
||||
Repositories repositories = bootstrapRepositories(ParentRepository.class);
|
||||
|
||||
assertRepositoryAvailableFor(repositories, Child.class, ParentRepository.class);
|
||||
}
|
||||
|
||||
@Test // GH-2406
|
||||
void usesChildRepositoryIfRegistered() {
|
||||
|
||||
Repositories repositories = bootstrapRepositories(ParentRepository.class, ChildRepository.class);
|
||||
|
||||
assertRepositoryAvailableFor(repositories, Child.class, ChildRepository.class);
|
||||
}
|
||||
|
||||
private void assertRepositoryAvailableFor(Repositories repositories, Class<?> domainTypem,
|
||||
Class<?> repositoryInterface) {
|
||||
|
||||
assertThat(repositories.hasRepositoryFor(domainTypem)).isTrue();
|
||||
assertThat(repositories.getRepositoryFor(domainTypem))
|
||||
.hasValueSatisfying(it -> assertThat(it).isInstanceOf(repositoryInterface));
|
||||
assertThat(repositories.getRepositoryInformationFor(domainTypem))
|
||||
.hasValueSatisfying(it -> assertThat(it.getRepositoryInterface()).isEqualTo(repositoryInterface));
|
||||
}
|
||||
|
||||
private Repositories bootstrapRepositories(Class<?>... repositoryInterfaces) {
|
||||
|
||||
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
|
||||
|
||||
for (Class<?> repositoryInterface : repositoryInterfaces) {
|
||||
beanFactory.registerBeanDefinition(repositoryInterface.getName(),
|
||||
getRepositoryBeanDefinition(repositoryInterface));
|
||||
}
|
||||
|
||||
context = new GenericApplicationContext(beanFactory);
|
||||
context.refresh();
|
||||
|
||||
return new Repositories(context);
|
||||
}
|
||||
|
||||
class Person {}
|
||||
|
||||
class Address {}
|
||||
@@ -301,4 +341,14 @@ class RepositoriesUnitTests {
|
||||
interface PrimaryRepository extends Repository<SomeEntity, Long> {}
|
||||
|
||||
interface ThirdRepository extends Repository<SomeEntity, Long> {}
|
||||
|
||||
// GH-2406
|
||||
|
||||
static class Parent {}
|
||||
|
||||
static class Child extends Parent {}
|
||||
|
||||
interface ParentRepository extends Repository<Parent, Long> {}
|
||||
|
||||
interface ChildRepository extends Repository<Child, Long> {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user