Skip to content

Commit 3955659

Browse files
Thomas Eizingerodrotbohm
Thomas Eizinger
authored andcommitted
DATACMNS-634 - Repositories now als returns repositories for super types of a domain class.
In case the repository lookup for a given domain type fails we traverse the given types super-types and try to detect a repository for those. Original pull request: #110.
1 parent 65f784b commit 3955659

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

src/main/java/org/springframework/data/repository/support/Repositories.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,6 +44,7 @@
4444
*
4545
* @author Oliver Gierke
4646
* @author Thomas Darimont
47+
* @author Thomas Eizinger
4748
*/
4849
public class Repositories implements Iterable<Class<?>> {
4950

@@ -138,11 +139,19 @@ private RepositoryFactoryInformation<Object, Serializable> getRepositoryFactoryI
138139

139140
Assert.notNull(domainClass, DOMAIN_TYPE_MUST_NOT_BE_NULL);
140141

141-
RepositoryFactoryInformation<Object, Serializable> repositoryInfo = repositoryFactoryInfos.get(ClassUtils
142-
.getUserClass(domainClass));
142+
Class<?> classToInspect = domainClass;
143+
RepositoryFactoryInformation<Object, Serializable> repositoryInfo = repositoryFactoryInfos
144+
.get(ClassUtils.getUserClass(classToInspect));
145+
146+
while (repositoryInfo == null && !classToInspect.equals(Object.class)) {
147+
classToInspect = classToInspect.getSuperclass();
148+
repositoryInfo = repositoryFactoryInfos.get(ClassUtils.getUserClass(classToInspect));
149+
}
150+
143151
return repositoryInfo == null ? EMPTY_REPOSITORY_FACTORY_INFO : repositoryInfo;
144152
}
145153

154+
146155
/**
147156
* Returns the {@link EntityInformation} for the given domain class.
148157
*

src/test/java/org/springframework/data/repository/support/RepositoriesUnitTests.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,25 +125,27 @@ public void shouldThrowMeaningfulExceptionWhenTheRepositoryForAGivenDomainClassC
125125
repositories.getCrudInvoker(EntityWithoutRepository.class);
126126
}
127127

128-
class Person {
129-
130-
}
131-
132-
class Address {
128+
/**
129+
* @see DATACMNS-634
130+
*/
131+
@Test
132+
public void findsRepositoryForSubTypes() {
133133

134+
Repositories repositories = new Repositories(context);
135+
assertThat(repositories.getPersistentEntity(AdvancedAddress.class), is(notNullValue()));
134136
}
135137

136-
class EntityWithoutRepository {
138+
class EntityWithoutRepository {}
137139

138-
}
140+
class Person {}
139141

140-
interface PersonRepository extends CrudRepository<Person, Long> {
142+
class Address {}
141143

142-
}
144+
class AdvancedAddress extends Address {}
143145

144-
interface AddressRepository extends Repository<Address, Long> {
146+
interface PersonRepository extends CrudRepository<Person, Long> {}
145147

146-
}
148+
interface AddressRepository extends Repository<Address, Long> {}
147149

148150
static class SampleRepoFactoryInformation<T, S extends Serializable> implements RepositoryFactoryInformation<T, S> {
149151

0 commit comments

Comments
 (0)