'java.sql.SQLException: Field 'id' doesn't have a default value with Many to Many relation

I am trying to do Many to many relation. I have UserEntity and NoteEntity. Note can be shared to many users, and user can have many notes from other users.

public class NoteEntity {
    @Id
    @Column(name = "id_note", nullable = false)
    private String id;
    @ManyToMany(mappedBy = "sharedToUserNotes")
    private Set<UserEntity> receivers = new HashSet<>();
}

public class UserEntity implements UserDetails {
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    @Column(name = "id_user")
    private String id;
    @ManyToMany(
        cascade = {
                CascadeType.MERGE, CascadeType.PERSIST
        })
    @JoinTable(name = "shared_notes",
        joinColumns = { @JoinColumn(name = "id_user") },
        inverseJoinColumns = { @JoinColumn(name = "id_note") })
    private Set<NoteEntity> sharedToUserNotes = new HashSet<>();

and when I am trying add note to set of notes and next save it:

public void addNoteToSharedToUserNotes(ShareForm shareForm) throws ValueNotFoundException{
    NoteEntity note = noteService.getNoteById(shareForm.getIdNote());
    UserEntity user = userService.getUserByLogin(shareForm.getUserLogin());
    user.getSharedToUserNotes().add(note);
    userEntityRepository.saveAndFlush(user);
}

. I'm getting error

java.sql.SQLException: Field 'id' doesn't have a default value

I think it is about additional table "shared_notes", becouse it has columns like: id, id_note, id_user and I can not find how to set value of that id.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source