title: Hibernate: Parent/Child relationship in a single-table
body: <p>I hardly see any pointer on the following problem related to Hibernate. This pertains to implementing inheritance using a single database table with a parent-child
  relationship to itself. For example:</p>
  
  <pre><code>CREATE TABLE Employee (
    empId BIGINT NOT NULL AUTO_INCREMENT,
    empName VARCHAR(100) NOT NULL,
    managerId BIGINT,
    CONSTRAINT pk_employee PRIMARY KEY (empId)
  )
  </code></pre>
  
  <p>Here, the <em>managerId</em> column may be null, or may point to another row of the <em>Employee</em> table. Business rule requires the Employee to know about all his reportees and for him to know about his/her manager. The business rules also allow rows to have null <em>managerId</em> (the CEO of the organisation doesn't have a manager).</p>
  
  <p>How do we map this relationship in Hibernate, standard many-to-one relationship doesn't work here? Especially, if I want to implement my Entities not only as a corresponding "Employee" Entity class, but rather multiple classes, such as "Manager", "Assistant Manager", "Engineer" etc, each inheriting from "Employee" super entity class, some entity having attributes that don't actually apply to all, for example "Manager" gets Perks, others don't (the corresponding table column would accept null of course).</p>
  
  <p>Example code would be appreciated (I intend to use Hibernate 3 annotations).</p>