title: Spring boot JPA Hibernate return nested Json Object : Reddit style comment system
body: <pre><code>@Entity
  public class Comment {
       @Id @GeneratedValue(strategy = GenerationType.IDENTITY)  
       private Long id;
       private String content;
  
  
       @ManyToOne(fetch = FetchType.EAGER)
          @JoinColumn(name = "parent_id")
          private Comment parentComment;
  
          @OneToMany(mappedBy="parentComment", cascade = CascadeType.ALL)
          private List&lt;Comment&gt; childrenComments = new ArrayList&lt;&gt;(); 
  }
  
  public interface CommentRepository extends JpaRepository&lt;Comment, Long&gt; {
  
  }
  </code></pre>
  
  <p>Here is how I am saving replies for a specific comment.</p>
  
  <pre><code>Comment parent = new Comment();
          parent.setContent(" 1 comment");
          parent = commentRepository.save(parent);
  
          Comment reply1 = new Comment();
          reply1.setContent("reply 1 to comment 1");
          reply1.setParentComment(parent);
  
          Comment reply2 = new Comment();
          reply1.setContent("reply 2 to comment 1");
          reply1.setParentComment(parent);
  
          parent = commentRepository.save(parent);
  </code></pre>
  
  <p>When I do commentrepository.findAll()</p>
  
  <p>I would like to return the following json data. </p>
  
  <pre><code>{
  
              "id": 1,
              "content": "1 comment",
              "replies" :[
                  {
                      "id": 2,
                      "content": "reply 1 to comment 1"
                  },
  
                  {
                      "id": 3,
                      "content": "reply 2 to comment 1"
                  }
  
                  ]
          }
  </code></pre>
  
  <p>My goal is to build a reddit style comment system. Where each comment has an array of comments(replies). When I query for all comments, using spring data jpa, I only want the root comments and their nested replies.</p>
  
  <p>In the above case, don't want something like this:</p>
  
  <pre><code>{
      {
  
          "id": 1,
          "content": "1 comment",
          "replies" :[
              {
                  "id": 2,
                  "content": "reply 1 to comment 1"
              },
  
              {
                  "id": 3,
                  "content": "reply 2 to comment 1"
              }
  
          ]
      }
  
      {
          "id": 2,
          "content": "reply 1 to comment 1"
       }
  
     {
          "id": 2,
          "content": "reply 1 to comment 1"
      }
  }
  </code></pre>
  
  <p>I have spent several days on this without success. I have tried using @jsonIgnore and other jackson annotations without success. Any suggestions or recommendations?
  Thank you in adavance. you are appreciated!</p>