Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
包含外键映射的POST REST请求_Rest_Spring Boot_Spring Data Jpa - Fatal编程技术网

包含外键映射的POST REST请求

包含外键映射的POST REST请求,rest,spring-boot,spring-data-jpa,Rest,Spring Boot,Spring Data Jpa,我是新来的。我正在尝试使用以下方法实现一个简单的REST api: -Springboot、JPA和rest以及hibernate 我有一个2表数据库,笔记本包含1到多个笔记 我已经设置了2个表和关系。我还创建了NotebookRepository和NoteRepository,以通过springboot rest获得基本的CRUD操作。数据库连接和关系正在运行 但是我不知道如何添加一个新的便笺(它有一个notebook_id外键,msut不能为NULL),每次我都试图按照这些思路发布一些东西

我是新来的。我正在尝试使用以下方法实现一个简单的REST api: -Springboot、JPA和rest以及hibernate

我有一个2表数据库,笔记本包含1到多个笔记 我已经设置了2个表和关系。我还创建了NotebookRepository和NoteRepository,以通过springboot rest获得基本的CRUD操作。数据库连接和关系正在运行 但是我不知道如何添加一个新的便笺(它有一个notebook_id外键,msut不能为NULL),每次我都试图按照这些思路发布一些东西
{ 标题:“abc”, “文本”:“无论什么”, “笔记本”:{ “id”:2 } }
我得到这个错误: 原因:java.sql.SQLIntegrityConstraintViolationException:列“notebook\u id”不能为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);
    }

@RepositoryRestResource(collectionResourceRel=“note”,path=“notes”)
公共接口NoteRepository扩展了JpaRepository{
//没有代码。。。
}
@RepositoryRestResource(collectionResourceRel=“notebook”,path=“notebooks”)
公共接口NotebookRepository扩展了JpaRepository{
}

我认为您需要为Notes类中笔记本字段的JoinColumn注释添加referencedColumnName=“id”


可能您的标识生成类型有问题。请参见

问题在于类
注意
没有带
笔记本
参数的构造函数来传递创建的笔记本对象,因此解决方案是添加此构造函数:

public Note(String title, String text, NoteBook noteBook) {
    this.title = title;
    this.text = text;
    this.noteBook = noteBook;
}
发送JSON对象就足够了,但要注意区分大小写:

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

数据库关系工作正常,“notebook”属性是一个id=1的嵌套notebook对象,用于引用note a对notebook的外键引用,其id为1。但我的应用程序仍然告诉我notebook\u id列为空。
@RepositoryRestResource(collectionResourceRel = "notebook", path = "notebooks")
public interface NotebookRepository extends JpaRepository<NoteBook, Integer>{

}

public Note(String title, String text, NoteBook noteBook) {
    this.title = title;
    this.text = text;
    this.noteBook = noteBook;
}
{ "title:"abc", "text":"whatever", "noteBook":{ "id":2 } }