【发布时间】:2020-08-08 11:04:42
【问题描述】:
@Entity
public class Product {
//..
private String name;
@OneToMany(mappedBy = "product", orphanRemoval = true)
private Set<File> files;
//..
}
@Entity
public class File {
//..
@ManyToOne
@JoinColumn(name = "product_id", nullable = true)
Product product;
//..
}
我只能从一侧创建关联,所以
POST /files/{id}
{
"product" : "http://localhost:8080/api/products/1"
}
有效但
POST /products/{id}
{
"files" : [
"http://localhost:8080/api/files/1"
]
}
不起作用。 POST 不会返回任何错误,但不会进行关联,并且 db 不会更新外键。
根据这个问题Post an entity with Spring Data REST which has relations 它应该可以工作,但不能。
编辑:添加了来自 https://www.baeldung.com/spring-data-rest-relationships 的附加示例页面
即使在该示例页面中,您也可以看到只能从“多”端进行关联。在那个例子中,他建立了一个图书馆书籍一对多关系,你唯一可以建立的关联如下:
curl -i -X PUT -H "Content-Type:text/uri-list"
-d "http://localhost:8080/libraries/1" http://localhost:8080/books/1/library
【问题讨论】:
标签: spring hibernate spring-data spring-data-rest