Spring Rest: Cannot insert rows that have @ManyToO

2020-02-16 02:26发布

I am following a spring getting started tutorial in https://spring.io/guides/gs/accessing-data-rest/

I added another entity books that has @ManyToOne relation to Person entity. Person entity has a new property called bikes and has a @OneToMany relation with Bike entity. Only thing that is different from the getting started project is the new attribute with its getter and 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 is described bellow:

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 did not change from the tutorial:

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 is the new repository i created that handles Bike requests:

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);
}

Looks pretty simple, right? I am supposed to be able to create a new Person using curl (just like in the tutorial):

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

And it works:

Post on localhost:8080/person that creates a new Person object

However, when i try to add a new Bike with the curl command:

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

I get the following error:

enter image description here

I get the same error even when providing a "person_id" propertly like so:

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

My question is: how can I add a new Bike?

2条回答
女痞
2楼-- · 2020-02-16 02:56

There is no way for your BikeRepository to know which row is represented by Person : { firstName : "Frodo", lastName : "Baggins" }. You have to tell your BikeRepository how to or where to query for that.

Try

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

That like will query http://localhost:8080/people/1 for the Person Entity.

It will also work with parameters based query, just replace the {id} query with http://localhost:8080/people?name='Frodo'

Hope that helps!

查看更多
疯言疯语
3楼-- · 2020-02-16 03:10

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

查看更多
登录 后发表回答