【问题标题】:Spring Rest: Cannot insert rows that have @ManyToOne columnSpring Rest:无法插入具有@ManyToOne 列的行
【发布时间】:2017-08-13 06:42:11
【问题描述】:

我正在关注https://spring.io/guides/gs/accessing-data-rest/ 中的春季入门教程

我添加了另一本与 Person 实体有 @ManyToOne 关系的实体书。 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;
    }
}

实体自行车描述如下:

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 是我创建的用于处理 Bike 请求的新存储库:

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 创建一个新人(就像在教程中一样):

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

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

【问题讨论】:

    标签: spring spring-mvc http-post spring-data-rest spring-hateoas


    【解决方案1】:

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

    【讨论】:

    • 谢谢你:D。我不知道这个组合怎么这么久都没有影响到我……
    • 您将如何更新 person_id 列上的自行车字段。我试过curl -i -X PUT -H "Content-Type:application/json" -d "{ \"name\" : \"Frodos Bike\", \"person\" : \"http://localhost:8080/people/2\" }" http://localhost:8080/bikes/1,但它不起作用。 PS有一个ID为2的人。
    • 为什么PUT 不起作用我不知道,我还没有找到答案。但如果您使用PATCH,它会起作用。这也是更新对象的更好选择,因为您只传输要更新的这些部分。 PUT 总是想要整个对象。 ...(顺便说一下,不需要http://localhost:8080
    • PUT 将与 curl -i -X PUT -H "Content-Type:application/json" -d '{"_links": {"person": {"href" : "/people/2"}}} http://localhost:8080/bikes/1/person 一起使用
    【解决方案2】:

    您的 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

    like 将在http://localhost:8080/people/1 中查询Person Entity

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

    希望有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-04
      • 2020-12-03
      相关资源
      最近更新 更多