[英]How to POST new nested entities using spring-data-rest
just wondering if it is possible to POST new entities within new entities. 只是想知道是否可以在新实体中发布新实体。
Person.java Person.java
@Entity
public class Person {
@oneToOne(optional = false)
private Address address;
}
Address.java Address.java
@Entity
public class Address {
private String street;
}
What I would like to do is create a Person with an Address in one HTTP request. 我想要做的是在一个HTTP请求中创建一个具有地址的Person。 Is this possible with something like the request below?
这可能与下面的请求有关吗?
curl -i -X POST -H "Content-Type: application/json" /
-d '{"address": {"street":"street 1"}}' http://localhost:8080/people
So far my investigation and document searching says no. 到目前为止,我的调查和文件检索说没有。 But thought I would ask here before giving up.
但是我想在放弃之前会问这里。
Thanks. 谢谢。
It works, but in your example you haven't turned on cascading. 它可以工作,但在你的例子中你还没有开启级联。
@OneToOne(optional = false, cascade = CascadeType.ALL)
private Address address;
Furthermore you must not have an exported repository for Address
. 此外,您不能拥有
Address
的导出存储库。 Otherwise Address
and Person
would be independent resources and had to be treated accordingly, which means separate POSTs. 否则
Address
和Person
将是独立的资源,必须相应地对待,这意味着单独的POST。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.