title: Spring data and Java Generics: How to combine multiple repository interfaces into one?
body: <p><strong>What I have now</strong><br>
  I have 2 repositores (infact many, but for this example lets say 2) and they look like  </p>
  
  <pre><code>@Repository
  public interface AgeRepository extends JpaSpecificationExecutor&lt;Age&gt;,
          JpaRepository&lt;Age, Long&gt; {
  } 
  </code></pre>
  
  <p>and </p>
  
  <pre><code>@Repository
  public interface GenderRepository extends JpaSpecificationExecutor&lt;Gender&gt;,
          JpaRepository&lt;Gender, Long&gt; {
  }
  </code></pre>
  
  <p>All the other repositories look same. The only difference is class name <code>Age, Gender, Network</code> etc  </p>
  
  <p>and they have similar implementations. The corresponding implementations for above two are  </p>
  
  <pre><code>@Component
  @Transactional
  public class AgeRepositoryService {
      private static final Logger LOGGER = LoggerFactory.getLogger(AgeRepositoryService.class);
      private AgeRepository ageRepository;
  
      @SuppressWarnings("UnusedDeclaration")
      public AgeRepositoryService() {
      }
  
      @Autowired
      public AgeRepositoryService(@Nonnull final AgeRepository ageRepository) {
          this.ageRepository = ageRepository;
      }
  
      @Nonnull
      public Age save(@Nonnull final Age age) {
          LOGGER.debug("adding age {}", age);
          return ageRepository.saveAndFlush(age);
      }
  
      @Nonnull
      public List&lt;Age&gt; getAges() {
          return ageRepository.findAll();
      }
  }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>@Component
  @Transactional
  public class GenderRepositoryService {
      private static final Logger LOGGER = LoggerFactory.getLogger(GenderRepositoryService.class);
      private GenderRepository genderRepository;
  
      @SuppressWarnings("UnusedDeclaration")
      public GenderRepositoryService() {
      }
  
      @Autowired
      public GenderRepositoryService(@Nonnull final GenderRepository genderRepository) {
          this.genderRepository = genderRepository;
      }
  
      @Nonnull
      public Gender save(@Nonnull final Gender gender) {
          LOGGER.debug("adding gender {}", gender);
          return genderRepository.saveAndFlush(gender);
      }
  
      @Nonnull
      public List&lt;Gender&gt; getGenders() {
          return genderRepository.findAll();
      }
  }
  </code></pre>
  
  <p>calling exactly same methods on repository interface.</p>
  
  <p><strong>What I did</strong><br>
  I created a generic repository interface as  </p>
  
  <pre><code>@Repository
  public interface GenericRepository&lt;T&gt; extends JpaSpecificationExecutor&lt;T&gt;,
          JpaRepository&lt;T, Long&gt; {
  }
  </code></pre>
  
  <p>and in one of the implementation class I tried as  </p>
  
  <pre><code>@Component
  @Transactional
  public class AgeRepositoryService {
      private static final Logger LOGGER = LoggerFactory.getLogger(AgeRepositoryService.class);
      private GenericRepository&lt;Age&gt; ageRepository;
  
      @SuppressWarnings("UnusedDeclaration")
      public AgeRepositoryService() {
      }
  
      @Autowired
      public AgeRepositoryService(@Nonnull final GenericRepository&lt;Age&gt; AgeRepository) {
          this.ageRepository = ageRepository;
      }
  
      @Nonnull
      public Age save(@Nonnull final Age age) {
          LOGGER.debug("adding age {}", age);
          return ageRepository.saveAndFlush(age);
      }
  
      @Nonnull
      public List&lt;Age&gt; getAges() {
          return ageRepository.findAll();
      }
  }
  </code></pre>
  
  <p>When I run my Tests, <code>maven</code> fails while loading the application context. I error I see is  </p>
  
  <pre><code>Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ageRepositoryService' defined in file [/Users/harith/IdeaProjects/comma/persistence/target/classes/com/yahoo/comma/persistence/impl/AgeRepositoryService.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.yahoo.comma.persistence.repository.GenericRepository]: : Error creating bean with name 'genericRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class java.lang.Object; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'genericRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not an managed type: class java.lang.Object
  </code></pre>
  
  <p>What am I doing wrong?  </p>
  
  <p><strong>What I am looking for?</strong><br>
  Generify the <code>Repositories</code> into one so that I do not have to write multiple generic classes just because they differ in class Name.</p>
  
  <p>How can I resolve such issues?</p>
  
  <p><strong>UPDATE</strong><br>
  My test looks like  </p>
  
  <pre><code>public class AgeRepositoryServiceTest extends AbstractUnitTestHelper {
  
      @Autowired
      AgeRepositoryService ageRepositoryService;
  
      @Test
      public void testGetAges() {
          Age age_13_17;
          {
              age_13_17 = new Age(1, "13-17", true, DateTime.now(), "pryme_user", DateTime.now(), "pryme_user");
              age_13_17 = ageRepositoryService.save(age_13_17);
              assertNotNull("Age Exists in Database", age_13_17.getId());
          }
  
          Age age_65_plus;
          {
              age_65_plus = new Age(1, "65+", true, DateTime.now(), "pryme_user", DateTime.now(), "pryme_user");
              age_65_plus = ageRepositoryService.save(age_65_plus);
              assertNotNull("Age Exists in Database", age_65_plus.getId());
          }
  
          {
              final List&lt;Age&gt; ages = ageRepositoryService.getAges();
              assertFalse(ages.isEmpty());
              assertEquals(2, ages.size());
  
              assertEquals(age_13_17.getAgeId(), ages.get(0).getAgeId());
              assertEquals(age_13_17.getName(), ages.get(0).getName());
              assertEquals("Age must be active", age_13_17.isActive(), ages.get(0).isActive());
              assertEquals(age_13_17.getCreatedAt(), ages.get(0).getCreatedAt());
              assertNotNull(ages.get(0).getCreatedBy());
              assertNotNull(ages.get(0).getUpdatedAt());
              assertNotNull(ages.get(0).getUpdatedBy());
  
              assertEquals(age_65_plus.getAgeId(), ages.get(1).getAgeId());
              assertEquals(age_65_plus.getName(), ages.get(1).getName());
              assertEquals("Age must be active", age_13_17.isActive(), ages.get(1).isActive());
              assertEquals(age_65_plus.getCreatedAt(), ages.get(1).getCreatedAt());
              assertNotNull(ages.get(1).getCreatedBy());
              assertNotNull(ages.get(1).getUpdatedAt());
              assertNotNull(ages.get(1).getUpdatedBy());
          }
      }
  }
  </code></pre>