title: Spring Data Mongo Repository:: Common shared method across all Repo issue
body: <p><strong>Use Case</strong></p>
  
  <p>I am trying to use <em>Adding custom behaviour to all repositories</em> functionality of Spring Data MongoDB.</p>
  
  <p>The documentation unhelpfully describes how to connect using JPA. Anyways got the config setup with Mongo equivalent. </p>
  
  <p>I want to add a findByCategoryName(String categoryName) method to all entities as all my entities will have a Category . Category is a DBRef object so have to use custom query.</p>
  
  <p>Below is relevant part of the config</p>
  
  <pre><code>&lt;!-- Activate Spring Data MongoDB repository support --&gt;
  &lt;mongo:repositories base-package="com.domain.*.repo" repository-impl-postfix="CustomImpl" 
      factory-class="com.domain.commonrepo.CommonMongoRepoFactoryBean"/&gt;
  
  &lt;bean id="mappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" /&gt;
  
  &lt;mongo:mapping-converter mapping-context-ref="mappingContext"&gt;
      &lt;mongo:custom-converters base-package="com.domain.mongo.converter" /&gt;
  &lt;/mongo:mapping-converter&gt;
  
  &lt;bean id="entityInformationCreator" class="org.springframework.data.mongodb.repository.support.DefaultEntityInformationCreator"&gt;
      &lt;constructor-arg name="mappingContext" ref="mappingContext" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>.
  .</p>
  
  <p><strong>The FactoryBean</strong></p>
  
  <pre><code>    @NoRepositoryBean
      public class CommonMongoRepoFactoryBean&lt;T extends MongoRepository&lt;?,?&gt;, ID extends        
      Serializable&gt; extends MongoRepositoryFactoryBean{
  
  @Autowired
  private static MongoTemplate mongoTemplate;
  
  protected MongoRepositoryFactory getRepositoryFactory(Class&lt;T&gt; clazz) {
      return new CommonMongoRepoFactory(clazz);
  }
  
  private static class CommonMongoRepoFactory extends MongoRepositoryFactory {
      private Class clazz;
  
      public CommonMongoRepoFactory(Class clazz) {
          super(mongoTemplate);
          this.clazz = clazz;
      }
  
      public CommonMongoRepoImpl getTargetRepository() {
          return new CommonMongoRepoImpl(clazz);
      }
  
      public Class&lt;?&gt; getRepositoryClass() {
          return CommonMongoRepoImpl.class;
      }
  }
  </code></pre>
  
  <p>I know it's a bit of a hack but with no documentation it is a pain. If anyone knows better PLEASE give me a github link :-)</p>
  
  <p><strong>Common Repo interface</strong></p>
  
  <pre><code>    @NoRepositoryBean
      public interface CommonMongoRepo&lt;T, ID extends Serializable&gt; extends MongoRepository&lt;T,ID&gt; {
  
  public List&lt;T&gt; findByCategoryName(String categoryName);        
  </code></pre>
  
  <p><strong>Implementation</strong></p>
  
  <pre><code>    @NoRepositoryBean
      public class CommonMongoRepoImpl&lt;T, ID extends Serializable&gt; extends SimpleMongoRepository&lt;T,    
      ID&gt; implements CommonMongoRepo&lt;T, ID&gt; {
  
  private Class&lt;T&gt; type;
  
  @Autowired
  private static MongoTemplate mongoOperations;
  
  @Autowired
  private static EntityInformationCreator entityInformationCreator;
  
  @Autowired
  private CategoryRepo categoryRepo;
  
  public CommonMongoRepoImpl(Class&lt;T&gt; type) { 
      super((MongoEntityInformation&lt;T, ID&gt;) entityInformationCreator.getEntityInformation(type), mongoOperations);
  }
  
  @Override
  public List&lt;T&gt; findByCategoryName(String categoryName) {
  
      Category category = categoryRepo.findByName(categoryName);
  
      return mongoOperations.find(query(where("categories.$id").is(category.getId())), type);
  }
  </code></pre>
  
  <p><strong>PROBLEM</strong></p>
  
  <p>Now when I am trying to use the common method I get an exception </p>
  
  <p>No Property category found in "Entity".  Which is I guess when mongo repo is trying to auto implement the method. This is inspite of me declaring the bean as @NoRepositoryBean</p>
  
  <p>PLEASE HELP!!! Dont want to add the same custom method to all the entities</p>