diff --git a/src/main/java/org/springframework/data/repository/support/Repositories.java b/src/main/java/org/springframework/data/repository/support/Repositories.java index 5542eb939d..70d5f5d68f 100644 --- a/src/main/java/org/springframework/data/repository/support/Repositories.java +++ b/src/main/java/org/springframework/data/repository/support/Repositories.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,6 +39,7 @@ * * @author Oliver Gierke * @author Thomas Darimont + * @author Thomas Eizinger */ public class Repositories implements Iterable> { @@ -133,11 +134,19 @@ private RepositoryFactoryInformation getRepositoryFactoryI Assert.notNull(domainClass, DOMAIN_TYPE_MUST_NOT_BE_NULL); - RepositoryFactoryInformation repositoryInfo = repositoryFactoryInfos.get(ClassUtils - .getUserClass(domainClass)); + Class classToInspect = domainClass; + RepositoryFactoryInformation repositoryInfo = repositoryFactoryInfos + .get(ClassUtils.getUserClass(classToInspect)); + + while (repositoryInfo == null && !classToInspect.equals(Object.class)) { + classToInspect = classToInspect.getSuperclass(); + repositoryInfo = repositoryFactoryInfos.get(ClassUtils.getUserClass(classToInspect)); + } + return repositoryInfo == null ? EMPTY_REPOSITORY_FACTORY_INFO : repositoryInfo; } + /** * Returns the {@link EntityInformation} for the given domain class. * diff --git a/src/test/java/org/springframework/data/repository/support/RepositoriesUnitTests.java b/src/test/java/org/springframework/data/repository/support/RepositoriesUnitTests.java index c08a8b0266..fbe3ee42d7 100644 --- a/src/test/java/org/springframework/data/repository/support/RepositoriesUnitTests.java +++ b/src/test/java/org/springframework/data/repository/support/RepositoriesUnitTests.java @@ -108,10 +108,22 @@ public void exposesPersistentEntityForDomainTypes() { assertThat(repositories.getPersistentEntity(Address.class), is(notNullValue())); } + /** + * @see DATAREST-321 + */ + @Test + public void findsRepositoryForSubTypes() { + + Repositories repositories = new Repositories(context); + assertThat(repositories.getPersistentEntity(AdvancedAddress.class), is(notNullValue())); + } + class Person {} class Address {} + class AdvancedAddress extends Address { } + interface PersonRepository extends CrudRepository {} interface AddressRepository extends Repository {}