Skip to content

Commit 297688c

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 dacf34e commit 297688c

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

+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.
@@ -41,6 +41,7 @@
4141
*
4242
* @author Oliver Gierke
4343
* @author Thomas Darimont
44+
* @author Thomas Eizinger
4445
*/
4546
public class Repositories implements Iterable<Class<?>> {
4647

@@ -135,11 +136,19 @@ private RepositoryFactoryInformation<Object, Serializable> getRepositoryFactoryI
135136

136137
Assert.notNull(domainClass, DOMAIN_TYPE_MUST_NOT_BE_NULL);
137138

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

151+
143152
/**
144153
* Returns the {@link EntityInformation} for the given domain class.
145154
*

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

+13-11
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)