Spring Rest:无法插入具有@ManyToOne列的行 [英] Spring Rest: Cannot insert rows that have @ManyToOne column
问题描述
我正在关注
然而,当我尝试添加一个使用curl命令的新自行车:
curl -i -X POST -HContent-Type:application / json-d {\name \:\Frodos Bike\,\person \:{\firstName \:\Frodo \,\lastName \ :\Baggins \}}http:// localhost:8080 / bikes
我收到以下错误:
即使提供person_id属性,我也会收到相同的错误:
curl -i -X POST -HContent-Type:application / json-d{\name \: \Frodos Bike\,\person_id \:\1 \}http:// localhost:8080 / bikes
我的问题是:如何添加新的自行车?
您的BikeRepository无法知道哪个行
由表示:人:{firstName:Frodo,lastName:Baggins}
。你必须告诉你的BikeRepository如何或在哪里查询。
试试
curl -i -X POST -HContent-Type:application / json-d{\name \:\ .Frodos Bike \,\person \:http: // localhost:8080 / people / 1}http:// localhost:8080 / bikes
这样会查询 http:// localhost:8080 / people / 1
用于人员实体
。
它也适用于基于参数的查询,只需用 http:// localhost:8080 / people替换
{id}
查询? name ='Frodo'
希望有所帮助!
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:
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:
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?
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!
这篇关于Spring Rest:无法插入具有@ManyToOne列的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!