Spring 休息:无法插入具有@ManyToOne 列的行

Spring Rest: Cannot insert rows that have @ManyToOne column

我正在关注 https://spring.io/guides/gs/accessing-data-rest/

中的 spring 入门教程

我添加了另一个与 Person 实体有@ManyToOne 关系的实体 books。 Person 实体有一个名为 bikes 的新 属性 并且与 Bike 实体有 @OneToMany 关系。与入门项目唯一不同的是新属性及其 getter 和 setter:

package hello;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

@Entity
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String firstName;
    private String lastName;

    @OneToMany
    private List<Bike> bikes;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public List<Bike> getBikes() {
        return bikes;
    }

    public void setBikes(List<Bike> bikes) {
        this.bikes = bikes;
    }
}

Entity Bike 描述如下:

package hello;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

@Entity
public class Bike {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String name;

    @ManyToOne
    @JoinColumn(nullable = false)
    private Person person;

    public String getName() {
        return name;
    }

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

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }
}

PersonRepository.java 与教程没有变化:

package hello;

import java.util.List;

import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {

    List<Person> findByLastName(@Param("name") String name);

}

BikeRepository.java 是我创建的处理自行车请求的新存储库:

package hello;

import java.util.List;

import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

@RepositoryRestResource(collectionResourceRel = "bikes", path = "bikes")
public interface BikeRepository extends PagingAndSortingRepository<Bike, Long> {
    List<Bike> findByName(@Param("name") String name);
}

看起来很简单,对吧?我应该能够使用 curl 创建一个新的 Person(就像在教程中一样):

curl -i -X POST -H "Content-Type:application/json" -d "{  \"firstName\" : \"Frodo\",  \"lastName\" : \"Baggins\" }" http://localhost:8080/people

有效:

但是,当我尝试使用 curl 命令添加新自行车时:

curl -i -X POST -H "Content-Type:application/json" -d "{  \"name\" : \"Frodos Bike\",  \"person\" : {  \"firstName\" : \"Frodo\",  \"lastName\" : \"Baggins\" } }" http://localhost:8080/bikes

我收到以下错误:

即使像这样正确地提供 "person_id",我也会遇到同样的错误:

curl -i -X POST -H "Content-Type:application/json" -d "{  \"name\" : \"Frodos Bike\",  \"person_id\" : \"1\" }" http://localhost:8080/bikes

我的问题是:如何添加新自行车?

试试 curl -i -X POST -H "Content-Type:application/json" -d "{ \"name\" : \"Frodos Bike\", \"person\" : \"/people/1\" }" http://localhost:8080/bikes

您的 BikeRepository 无法知道哪个 rowPerson : { firstName : "Frodo", lastName : "Baggins" } 表示。您必须告诉您的 BikeRepository 如何或在哪里查询它。

尝试

curl -i -X POST -H "Content-Type:application/json" -d "{ \"name\" : \Frodos Bike\", \"person\" : "http://localhost:8080/people/1" }" http://localhost:8080/bikes

这将查询 http://localhost:8080/people/1 以获得 Person Entity

它也适用于基于参数的查询,只需将 {id} 查询替换为 http://localhost:8080/people?name='Frodo'

希望对您有所帮助!