title: Spring Data Mongodb - repository for collection with different types
body: <p>I have a mongo collection that may contain three types of entities that I map to java types:</p>
  
  <ul>
  <li>Node</li>
  <li>LeafType1</li>
  <li>LeafType2</li>
  </ul>
  
  <p>Collection is ment to store tree-like structure using dbRefs of child nodes in parent entry.</p>
  
  <p>I didn't find any information about subject in Spring reference documentation so I'm asking here: Is there a way to use <code>Repository</code> mechanism to work with collection that may contain different types of objects?</p>
  
  <p>Declaring several repositories for different types in one collection seems like not very good idea because I always struggle with situations when queried object is not of expected type and creating one repository for abstract class that all possible types inherrit doesn't seems to work.</p>
  
  <p>To illustrate what I mean:</p>
  
  <pre><code>/**
   * This seems not safe
   */
  public interface NodeRepository extends MongoRepository&lt;Node, String&gt; { }
  public interface LeafType1Repository extends MongoRepository&lt;LeafType1, String&gt; { }
  public interface LeafType2Repository extends MongoRepository&lt;LeafType2, String&gt; { }
  
  /**
   * This doesn't work at all
   */
  public interface MyCollectionRepository extends MongoRepository&lt;AbstractMyCollectionNode, String&gt; { }
  </code></pre>