我正在使用带有存储库的 spring data rest,db 是 mysql。
我有对象父子。关系是多对一。
子级与父级关系是单向的。我在父级中没有子对象列表。
Parent{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected Long id = null;
@NotNull(message = "name of user should not be null")
@Size(min=2, max=30)
private String name;
}
Child{
private String name;
@ManyToOne
private Parent parent;
}
我有一个父级"1",我想将其引用给新创建的孩子。新子"POST"请求的 json 输入为
{
"name":"child name",
"parent":{
"id":1
}
}
在新创建的子项中关联父项"1"时需要帮助。
json 格式是否需要任何更改?我也尝试使用"parent_id",但仍然有错误。
由于 spring-data-rest 使用 HATEOAS。以下格式有效。
{
"name":"testname1",
"parent": "http://localhost:8080/parent/2"
}
感谢所有试图回答的人。