title: Hibernate same table parent / child relation
body: <p>I have Parent/Child relationship in a single-table, let's say as simple as</p>
  
  <pre><code>id | parent_id | some_data
  </code></pre>
  
  <p>I am trying to understand / implement best practices how to build Hibernate classes for single table relationship. </p>
  
  <p>I have a feed that comes from somewhere and it has nested JSON structure, so after parse it I want to have it representation in OracleDB.</p>
  
  <p>Feed looks like</p>
  
  <pre><code>{
     1:
         =&gt; 2:
                =&gt; 3
                =&gt; 4:
                        =&gt; 5
         =&gt; 6  
  }
  </code></pre>
  
  <p>this data should end up in db as :</p>
  
  <pre><code>1 | 0 (main rec, no parent)
  2 | 1
  3 | 2
  4 | 2
  5 | 4
  6 | 1 
  </code></pre>
  
  <p>And it could go deeper and deeper... </p>
  
  <p>I would like to traverse JSON structure and build classes that at the end I save in db </p>
  
  <pre><code>session.save(parent)
  </code></pre>
  
  <p>parent would be instance of my hibernate mapped class, lets name it Feed.</p>
  
  <p>Each time I descend a tree, it creates a new Feed, finds it's parent and add it to the list. </p>
  
  <pre><code>Feed **parent** = new Feed();
  
    -&gt;   Feed child2 = new Feed();
         child2.setParent(parent)
         parent.add(child2); 
  
        -&gt;   Feed child3 = new Feed();
             child3.setParent(child2)
             child2.add(child3);
  
             Feed child4 = new Feed();
             child4.setParent(child2)
             child2.add(child4);
  
  ............. 
  
  session.save(**parent**)
  </code></pre>
  
  <p>My question is could I use @ManyToOne and @OneToMany approach? </p>
  
  <p>I also looked at @Inheritance(strategy = InheritanceType.SINGLE_TABLE) but I do not think I could apply it to my problem since I do not know parents, they are dynamic. I could not build parent class and extend it. </p>
  
  <p>So bottom line I am trying to solve two problems</p>
  
  <ol>
  <li><p>Easy save on a parent level</p></li>
  <li><p>How to get data from a parent to a leaf when I need it</p></li>
  </ol>
  
  <p>Some Hibernate Mapped Classes example would be very appreciated.</p>