title: Multi-level sub-menu JSON in Spring boot
body: <p>I have a some records in the table having parent child relations, screenshot below:
  <a href="https://i.stack.imgur.com/Y3orL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Y3orL.png" alt="enter image description here"></a></p>
  
  <p>How do I write a JPA Entity to retrieve those records with respect to Parent-Child relation. Your help is appreciated.</p>
  
  <p>The code that did not help me well is as below:</p>
  
  <pre><code>@Entity
  @Table(name = PlatformConstant.TABLE_MENU)
  @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
  public class Menu implements Serializable {
  
     private static final long serialVersionUID = 1L;
  
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     private Long id;
  
     @NotNull
     @Column(name = "url", nullable = false)
     private String url;
  
     @Column(name = "description")
     private String description;
  
     @Column(name = "qr_code")
     private Blob qrCode;
  
     @JsonManagedReference
     @OneToMany(mappedBy = "parent")
     private Set&lt;Menu&gt; children;
  
     @ManyToOne
     @JsonBackReference
     private Menu parent; 
  }
  </code></pre>
  
  <blockquote>
    <p>My code above has the following wrong output:
    <a href="https://i.stack.imgur.com/f5aRX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/f5aRX.png" alt="enter image description here"></a></p>
  </blockquote>
  
  <p>Using the JpaRepository find all, and applying the answer from @lucid, the new output is as below:</p>
  
  <p>the code: </p>
  
  <pre><code>@Autowired
  private MenuService menuService;
  
  @CrossOrigin
  @GetMapping("/all")
  @ResponseBody
  public List&lt;Menu&gt; getMenus() {
      return (List&lt;Menu&gt;) menuService.findAll().stream()
               .filter (menu-&gt; Objects.isNull(menu.getParent()).collect(Collectors.toList()));
  }
  </code></pre>
  
  <p>the output:</p>
  
  <p><a href="https://i.stack.imgur.com/czjXy.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/czjXy.png" alt="enter image description here"></a></p>
  
  <p>Thank you.</p>