Using Comparable To Compare and sort TreeSet in Descending Of JPA Entity Id

I am trying to sort my TreeSet using JPA Auto Generated Id. Here, Note is an Entity class.

@Entity
@Table(name="NOTE")
@Getter
@Setter
public class Note implements Comparable<Note> {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;
    private String note;
    @ManyToOne
    @JoinColumn(name = "noteDetailsId")
    private NoteDetails noteDetails;
     
   @override
   public int compareTo(Note myNote) {
   return myNote.getId().intValue() - this.getId().intValue();
  }
   
}

@Entity
@Table(name="NOTE_DETAILS")
@Getter
@Setter

class NoteDetails  {
  @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;
@OneToMany(mappedBy = "noteOperation", cascade = Cascadetype.ALL, orphanRemoval = true, fetch=fetchType.EAGER)
private Set<Note> notes = new TreeSet<>();

public void addNote(String note) {
   this.notes.add(new Note().setNote("note1"));
}

}

My problem is that, I am getting JPA Ids as null in compareTo() method. Hence thowing nullPointerException. My purpose is to sort TreeSet in descending order of JPA Ids. But those Ids are coming null.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum