1

i'm new to Springboot. I'm trying to implement a simple REST api using : -Springboot, JPA & rest along with hibernate

I have a 2 tables database, Notebook that contains 1 to many notes I already setup the 2 tables and relationships. I also created a NotebookRepository and NoteRepository to get basic CRUD operations via the springboot rest. The Database connection and relationships are functionning but i don't know how to add a new note (it has a notebook_id foreign key which msut NOT be NULL) and everytime i tryto post something along these lines
{ "title:"abc", "text":"whatever", "notebook":{ "id":2 } }
i get this error : Caused by: java.sql.SQLIntegrityConstraintViolationException: Column 'notebook_id' cannot be null

@Entity
@Table(name="notebook")
public class NoteBook {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private int id;

    @Column(name="name")
    private String name;

    @OneToMany(mappedBy="notebook", cascade=CascadeType.ALL)
    List<Note> notes;

    public NoteBook() {

    }

    public NoteBook(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Note> getNotes() {
        return notes;
    }

    public void setNotes(List<Note> notes) {
        this.notes = notes;
    }

    public void addNote(Note note) {
        if(notes == null) {
            notes = new ArrayList<>();
        }
        note.setNotebook(this);
        notes.add(note);
    }

@Entity
@Table(name="note")
public class Note {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private int id;

    @Column(name="title")
    private String title;

    @Column(name="text")
    private String text;

    @ManyToOne(cascade={CascadeType.MERGE, CascadeType.DETACH, CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinColumn(name="notebook_id")
    private NoteBook notebook;

    public Note() {

    }

    public Note(String title, String text) {
        this.title = title;
        this.text = text;
    }

@RepositoryRestResource(collectionResourceRel = "note", path = "notes")
public interface NoteRepository extends JpaRepository<Note, Integer>{
    //No code...
}

@RepositoryRestResource(collectionResourceRel = "notebook", path = "notebooks")
public interface NotebookRepository extends JpaRepository<NoteBook, Integer>{

}

user7616817
  • 357
  • 4
  • 18

2 Answers2

1

The problem is that the class Note doesn't have a constructor with NoteBook parameter to pass the created NoteBook object to, so the solution is to add this constructor:

public Note(String title, String text, NoteBook noteBook) {
    this.title = title;
    this.text = text;
    this.noteBook = noteBook;
}

and it's enough to send the JSON object as you do, but just be aware of case-sensitivity:

{ "title:"abc", "text":"whatever", "noteBook":{ "id":2 } }
0

I think you need to add referencedColumnName = "id" for JoinColumn annotation for notebook field in Note class.

Maybe you have problem with IDENTITY generation type. See this problem with null pointer

zdadco
  • 102
  • 9
  • The database relationship works just fine, the problem i'm having is in my POST Body Request, the "notebook" property is a nested notebook object with id = 1 to reference that the note A has a foregn key reference to notebook wich id is 1. But my application still tells me that notebook_id column is null. – user7616817 Mar 25 '19 at 15:04