title: Spring Jpa adding custom functionality to all repositories and at the same time other custom funcs to a single repository
body: <p>Spring documentation here <a href="http://docs.spring.io/spring-data/data-jpa/docs/current/reference/html/repositories.html#repositories.custom-implementations" rel="noreferrer">http://docs.spring.io/spring-data/data-jpa/docs/current/reference/html/repositories.html#repositories.custom-implementations</a> gives example to add custom functionalities to all repositories or to a single repositories, not both.</p>
  
  <p>Let suppose I want to add some custom funcs to all repositories (using Custom Repository Factory Bean) and some other only to a single repositories (docs says to use a Custom Interface and a Custom Impl); how can I achieve this?</p>
  
  <p>Some example code where I added "setCurrentTenansInSession" method to all repositories; now I want to add a custom method, e.g. "newCustomMethod", to ona single repository (that is a MyJpaRepository, as for my custom repository factory). How do I do this?</p>
  
  <p>Custom behaviour interface:</p>
  
  <pre><code>@NoRepositoryBean
  public interface MyJpaRepository&lt;T, ID extends Serializable&gt; extends JpaRepository&lt;T, ID&gt; { 
      public void setCurrentTenantInSession(Object object);       
  }
  </code></pre>
  
  <p>Custom behaviour implementation:</p>
  
  <pre><code>public class MultiTenantSimpleJpaRepository&lt;T, ID extends Serializable&gt; extends SimpleJpaRepository&lt;T, ID&gt; implements MyJpaRepository&lt;T, ID&gt; {
      public void setCurrentTenantInSession(Object object) {
          //custom impl
      }
  }
  </code></pre>
  
  <p>Custom repository factory bean :</p>
  
  <pre><code>public class MultiTenantJpaRepositoryFactoryBean&lt;T extends Repository&lt;S, ID&gt;, S, ID extends Serializable&gt; extends JpaRepositoryFactoryBean&lt;T, S, ID&gt; {
  
      @Override
      protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager) {
          return new MultiTenantJpaRepositoryFactory(entityManager);
      }
  }
  </code></pre>
  
  <p>And finally the custom repository factory :</p>
  
  <pre><code>public class MultiTenantJpaRepositoryFactory extends JpaRepositoryFactory {
      public MultiTenantJpaRepositoryFactory(EntityManager entityManager) {
          super(entityManager);
      }
  
      @Override
      protected JpaRepository&lt;?, ?&gt; getTargetRepository(RepositoryMetadata metadata, EntityManager entityManager) {
          final JpaEntityInformation&lt;?, Serializable&gt; entityInformation = getEntityInformation(metadata.getDomainType());
  
          final SimpleJpaRepository&lt;?, ?&gt; repo = new MultiTenantSimpleJpaRepository(entityInformation, entityManager);
  
          repo.setLockMetadataProvider(LockModeRepositoryPostProcessor.INSTANCE.getLockMetadataProvider());
          return repo;
      }
  
      @Override
      protected Class&lt;?&gt; getRepositoryBaseClass(RepositoryMetadata metadata) {
          return MultiTenantSimpleJpaRepository.class;
      }
  }
  </code></pre>