title: Parent - Child relationship - self join mapping
body: <p>I'm trying to build an application which will receive the XML file with list of Employees and store the parent-child/employee-manager relation in the single database table.</p>
  
  <p>My XML file looks like this one:</p>
  
  <pre><code>&lt;Employees&gt;
      &lt;Employee manager="Patrick"&gt;Martin&lt;/Employee&gt;
      &lt;Employee manager="Patrick"&gt;Kent&lt;/Employee&gt;
      &lt;Employee manager="Martin"&gt;Mark&lt;/Employee&gt;
      &lt;Employee&gt;Hugo&lt;/Employee&gt; &lt;!-- root element of the employee-manager tree --&gt;
      &lt;Employee manager="Hugo"&gt;Osa&lt;/Employee&gt;
      &lt;Employee manager="Osa"&gt;Patrick&lt;/Employee&gt;
  &lt;/Employee&gt;
  </code></pre>
  
  <p>One employee can have only one manager, but one manager can have multiple subordinates/employees.</p>
  
  <p>I have no troubles when unmarshalling the received XML file but now I'm trying to create the appropriate model which will allow me to store the unmarshalled values in the database. Data should be stored in the table named "Employee" and should contain following data:</p>
  
  <pre><code>------------------------------
  | id            | Integer    |
  ------------------------------
  | employee_name | String     |
  ------------------------------
  | parent_id     | Integer    | -- reference to the manager
  ------------------------------
  </code></pre>
  
  <p>I created a new class named <code>Employee</code> but I'm not sure how to define appropriate ManyToOne/OneToMany annotations. </p>
  
  <p>Since I'm fairly new to this, I've Googled couple of examples and tutorials (as well as the answers on the similar questions here on Stack Overflow), but I guess I'm making some big mistake in this implementation when defining this model. My latest try looks like this:</p>
  
  <pre><code>public class Employee {
      @Id
      @GeneratedValue
      private int id;
  
      @Column(name = "parent_id")
      @Transient
      @ManyToOne(cascade={CascadeType.ALL})
      private String managerName;
  
      @Column(name = "employee_name")
      @JoinColumn(name="parent_id")
      private String employeeName;
  
      // getters and setters
  </code></pre>
  
  <p>If anyone could point me in the the direction of defining appropriate model, it would be much, much appreciated!</p>