title: How to design hibernate entity for parent child response in json format from single database table
body: <p>I have database table </p>
  
  <pre><code>+-------------+----------------------+--------+
  | category_id | name                 |parent_id  |
  +-------------+----------------------+--------+
  |           1 | ELECTRONICS          |   NULL |
  |           2 | TELEVISIONS          |      1 |
  |           3 | TUBE                 |      2 |
  |           4 | LCD                  |      2 |
  |           5 | PLASMA               |      2 |
  |           6 | PORTABLE ELECTRONICS |      1 |
  |           7 | MP3 PLAYERS          |      6 |
  |           8 | FLASH                |      7 |
  |           9 | CD PLAYERS           |      6 |
  |          10 | 2 WAY RADIOS         |      6 |
  +-------------+----------------------+--------+
  </code></pre>
  
  <p>and i designed hibernate entity this way but i am not able to get expected output which is mentioned in below json format:</p>
  
  <pre><code>@Entity
  @Table(name = "category")
  public class Category {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "category_id")
    private short categoryId;
    @Column(name = "name")
    private String name;
  
    @Column(name = "parent_id")
    private Integer parentId;
  
    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "parent_id")
    private Set&lt;Category&gt; child;
  
    public short getCategoryId() {
          return categoryId;
    }
  
    public void setCategoryId(short categoryId) {
          this.categoryId = categoryId;
    }
  
    public String getName() {
          return name;
    }
  
    public void setName(String name) {
          this.name = name;
    }
  
    public Integer getParentId() {
          return parentId;
    }
  
    public void setParentId(Integer parentId) {
          this.parentId = parentId;
    }
  
    public Set&lt;Category&gt; getChild() {
          return child;
    }
  
    public void setChild(Set&lt;Category&gt; child) {
          this.child = child;
    }
    }
  </code></pre>
  
  <p>Now i want response in below json format </p>
  
  <pre><code>[
  {
  "categoryId": 1,
  "name": "ELECTRONICS",
  "child": [
    {
      "categoryId": 2,
      "name": "TELEVISIONS",
      "child": [
        {
          "categoryId": 3,
          "name": "TUBE",
          "child": []
        },
        {
          "categoryId": 4,
          "name": "LCD",
          "child": []
        },
        {
          "categoryId": 5,
          "name": "PLASMA",
          "child": []
        }
      ]
    },
    {
      "categoryId": 6,
      "name": "PORTABLE ELECTRONICS",
      "child": [
        {
          "categoryId": 7,
          "name": "MP3 PLAYERS ",
          "child": [
            {
              "categoryId": 8,
              "name": "FLASH",
              "child": []
            }
          ]
        },
        {
          "categoryId": 9,
          "name": "CD PLAYERS",
          "child": []
        },
        {
          "categoryId": 10,
          "name": "2 WAY RADIOS",
          "child": []
        }
      ]
    }
  ]
  }
  ]
  </code></pre>
  
  <p>When i am trying to list category list using this code and sending response then i am getting below exception :</p>
  
  <pre><code>@RequestMapping(value = "/fetchCategory", method = RequestMethod.GET)
    public List&lt;Category&gt; fetchCategory() {
      List&lt;Category&gt; categories = null;
          try {
              categories = masterConfigurationService.fetchCategory();
          } catch (ServiceException e) {
                LOGGER.error(e.getMessage(), e);
          }
          return categories;
    }
  </code></pre>
  
  <p>And DAO code is</p>
  
  <pre><code>session.createCriteria(Category.class).list();
  </code></pre>
  
  <p>UPDATE : 
  - Updated entity - removed parent mapping from entity</p>
  
  <p>Now i am getting parent tree two times if it is associated with two child.</p>