Skip to content

Commit 9f21cc2

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 b2251aa commit 9f21cc2

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

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

+12-3
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.
@@ -39,6 +39,7 @@
3939
*
4040
* @author Oliver Gierke
4141
* @author Thomas Darimont
42+
* @author Thomas Eizinger
4243
*/
4344
public class Repositories implements Iterable<Class<?>> {
4445

@@ -133,11 +134,19 @@ private RepositoryFactoryInformation<Object, Serializable> getRepositoryFactoryI
133134

134135
Assert.notNull(domainClass, DOMAIN_TYPE_MUST_NOT_BE_NULL);
135136

136-
RepositoryFactoryInformation<Object, Serializable> repositoryInfo = repositoryFactoryInfos.get(ClassUtils
137-
.getUserClass(domainClass));
137+
Class<?> classToInspect = domainClass;
138+
RepositoryFactoryInformation<Object, Serializable> repositoryInfo = repositoryFactoryInfos
139+
.get(ClassUtils.getUserClass(classToInspect));
140+
141+
while (repositoryInfo == null && !classToInspect.equals(Object.class)) {
142+
classToInspect = classToInspect.getSuperclass();
143+
repositoryInfo = repositoryFactoryInfos.get(ClassUtils.getUserClass(classToInspect));
144+
}
145+
138146
return repositoryInfo == null ? EMPTY_REPOSITORY_FACTORY_INFO : repositoryInfo;
139147
}
140148

149+
141150
/**
142151
* Returns the {@link EntityInformation} for the given domain class.
143152
*

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

+12
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,22 @@ public void exposesPersistentEntityForDomainTypes() {
108108
assertThat(repositories.getPersistentEntity(Address.class), is(notNullValue()));
109109
}
110110

111+
/**
112+
* @see DATACMNS-634
113+
*/
114+
@Test
115+
public void findsRepositoryForSubTypes() {
116+
117+
Repositories repositories = new Repositories(context);
118+
assertThat(repositories.getPersistentEntity(AdvancedAddress.class), is(notNullValue()));
119+
}
120+
111121
class Person {}
112122

113123
class Address {}
114124

125+
class AdvancedAddress extends Address {}
126+
115127
interface PersonRepository extends CrudRepository<Person, Long> {}
116128

117129
interface AddressRepository extends Repository<Address, Long> {}

0 commit comments

Comments
 (0)