title: How to sort the self-reference structure?
body: <p>In my Spring Boot application, there is the model &quot;Item&quot; which has sefl-reference. I would like to realize sorting by field sortOrder, but I can't understand how to implement it for CRUD operations.
  For instance, how to get the value for sortOrder before saving my entity to the repository and how to recalculate sortOrder after updating the entity. Any help will be highly appreciated! Thanks!</p>
  <pre><code>@Data
  @Entity
  @NoArgsConstructor
  @AllArgsConstructor
  @Table(name = &quot;item&quot;)
  public class Item {
  
      @Id
      @JsonFormat(shape = JsonFormat.Shape.STRING)
      @SequenceGenerator(name = &quot;itemSeq&quot;, sequenceName = &quot;item_id_seq&quot;, allocationSize = 50)
      @GeneratedValue(strategy = GenerationType.IDENTITY, generator = &quot;itemSeq&quot;)
      private Long id;
  
      @NotBlank
      @Column(name = &quot;name&quot;)
      private String name;
  
      @Column(name = &quot;description&quot;)
      private String description;
  
      @Column(name = &quot;sort_order&quot;)
      private String sortOrder;
  
      @Column(name = &quot;is_active&quot;)
      private Boolean isActive;
  
      @ManyToOne
      @JoinColumn(name = &quot;parent_id&quot;)
      @JsonFormat(shape = JsonFormat.Shape.STRING)
      @JsonIgnoreProperties({&quot;name&quot;, &quot;description&quot;, &quot;sortOrder&quot;, &quot;isActive&quot;, &quot;parent&quot;, &quot;children&quot;})
      private Item parent;
  
      @OneToMany(mappedBy = &quot;parent&quot;, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
      @Where(clause = &quot;is_active = true&quot;)
      @Fetch(FetchMode.SUBSELECT)
      @OrderBy(&quot;sort_order ASC&quot;)
      private List&lt;Item&gt; children = new ArrayList&lt;&gt;();
  }
  </code></pre>