title: How do I map relationships between entities of the same super class using JPA?
body: <p>I need to establish relationships between two entities using JPA/Hibernate: <code>Child</code> and <code>Father</code>, both extends the entity <code>User</code>. A father can have none to n children, and every children has to have a father. I can insert a father with no problem, but can't add a child that belongs to the father I have inserted before. Here is how I add a child:</p>
  
  <pre><code>    SelectStatement ss = new SelectStatement();
      ss.manager.clear();
      Father father =  ss.getFather(socialId);
  
      Child child = new Child();
      //Set some parameters
      dependente.setFather(father);
  
      titular.addChild(child);
      ss.manager.refresh(titular);
      ss.manager.persist(dependente);
      ss.manager.getTransaction().begin();
      try{
          ss.manager.getTransaction().commit();
          ss.close();
          return true;
      }
      catch(RollbackException e){
          ss.close();
          return false;
      }
  </code></pre>
  
  <p>Class User:</p>
  
  <pre><code>   @Entity
     @Inheritance ( strategy = InheritanceType.JOINED )
     public class User{
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO) 
      private int pk_user;
  
      @Column
      private String name;        
  }
  </code></pre>
  
  <p>Class Father:</p>
  
  <pre><code>@Entity
  public class Father extends User{
  
  
      @OneToMany(mappedBy="father")
      @LazyCollection(LazyCollectionOption.FALSE)
      private Collection &lt;Child&gt; childs;
  
      @Column(nullable=true, unique=true)
      private String social_id;
  
  
      public void addChild(Child child){
          this.childs.add(child);
      }       
  }
  </code></pre>
  
  <p>Class Child:</p>
  
  <pre><code>@Entity
  public class Child extends User{
  
      //Here is the problem!
      @ManyToOne(optional = false)
      @JoinColumn(name = "pk_user")
      private Father father;
  
  }
  </code></pre>
  
  <p>PS: I have erased/changed some attributes for the sake of clarity, so don't worry about non-existent get/set methods, etc.</p>
  
  <p>The following code produce the error "should use  insertable = false, updatable = false". If I do use, I receive the error "Cannot add or update a child row".</p>
  
  <p>I can erase the class User and copy all their attributes to Father and Child, creating a solution to this problem. But my real Father/Child/User classes are complex enough to avoid doing that.</p>
  
  <p>Thank you.</p>