title: How is entities inheritance implemented in Spring data mongodb
body: <p>I've two entities Person, Employee and Employee1. I want to implement entities inheritance in Spring Data MongoDB. Like in Spring Data JPA, what are the equivalent annotations for @Inheritance and @PrimaryKeyJoinColumn in Spring Data MongoDB. Right now, I've implemented something like this:</p>
  
  <pre><code>interface Person {
      String getId();
      void setId(String id);
  
      String getName();
      void getName(String name);
  }
  
  @Document(collection = "person")
  class PersonImpl implements Person {
      @Id
      String id;
      // Getters and setters 
      // Constructors, equals, hashcode and toString methods
  }
  
  interface Employee extends Person {
      int getNumberOfDependents();
      void getNumberOfDependents(int numberOfDependents);
  }
  
  @Document(collection = "employee")
  class EmployeeImpl extends PersonImpl implements Employee {
      // Getters and setters 
      // Constructors, equals, hashcode and toString methods
  }
  
  interface Employee1 extends Person {
      int getNumberOfDependents();
      void getNumberOfDependents(int numberOfDependents);
  }
  
  @Document(collection = "employee1")
  class Employee1Impl extends PersonImpl implements Employee1 {
      // Getters and setters 
      // Constructors, equals, hashcode and toString methods
  }
  
  Repository structure:
  public interface PersonRepository extends MongoRepository&lt;Person, String&gt; {
  }
  
  public interface EmployeeRepository extends MongoRepository&lt;Employee, String&gt; {
  }
  
  public interface Employee1Repository extends MongoRepository&lt;Employee1, String&gt; {
  }
  </code></pre>
  
  <p>I'm saving the Person object first and then taking the ID of it and creating an Employee object with the same ID and saving it. This creates new object and hence I'm losing all the Person object stuff. 
  I also feel that I've to get the NoRepositoryBean implemented also. 
  I'm confused. Please help.</p>