From 39556590c34286b5f46bb7a64060af15b190a498 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Sun, 18 Jan 2015 22:41:10 +0100 Subject: [PATCH] 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. --- .../data/repository/support/Repositories.java | 15 +++++++++--- .../support/RepositoriesUnitTests.java | 24 ++++++++++--------- 2 files changed, 25 insertions(+), 14 deletions(-) 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 cdf80f304..b5ba4a36e 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. @@ -44,6 +44,7 @@ import org.springframework.util.ClassUtils; * * @author Oliver Gierke * @author Thomas Darimont + * @author Thomas Eizinger */ public class Repositories implements Iterable> { @@ -138,11 +139,19 @@ public class Repositories implements Iterable> { 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 fa6e8756e..078fb3550 100644 --- a/src/test/java/org/springframework/data/repository/support/RepositoriesUnitTests.java +++ b/src/test/java/org/springframework/data/repository/support/RepositoriesUnitTests.java @@ -125,25 +125,27 @@ public class RepositoriesUnitTests { repositories.getCrudInvoker(EntityWithoutRepository.class); } - class Person { + /** + * @see DATACMNS-634 + */ + @Test + public void findsRepositoryForSubTypes() { + Repositories repositories = new Repositories(context); + assertThat(repositories.getPersistentEntity(AdvancedAddress.class), is(notNullValue())); } - class Address { + class EntityWithoutRepository {} - } + class Person {} - class EntityWithoutRepository { + class Address {} - } + class AdvancedAddress extends Address {} - interface PersonRepository extends CrudRepository { + interface PersonRepository extends CrudRepository {} - } - - interface AddressRepository extends Repository { - - } + interface AddressRepository extends Repository {} static class SampleRepoFactoryInformation implements RepositoryFactoryInformation {