我想通过RESTFUl服务在嵌套关系的窗体中保存2个对象。也就是说,一个办公室有2名员工。如何在春天保存嵌套的json对象mvc
然而,正如你可以从下面的示例中看到的,没有保存的办公室,我就无法知道officecode为20.在不:
“officecode”:“20”
虽然json对象是嵌套的,但是,我保存了员工和办公室,但他们没有关联。
如何保存嵌套对象在1提交然后?
实体的样子:
// Property accessors
@Id
@Column(name = "OFFICECODE", unique = true, nullable = false, length = 10)
public String getOfficecode() {
return this.officecode;
}
....
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "office")
public Set<Employee> getEmployees() {
return this.employees;
}
这里是REST调用保存办公室:
@RequestMapping(value = "/Office", method = RequestMethod.POST)
@ResponseBody
public Office newOffice(@RequestBody Office office) {
officeService.saveOffice(office);
return officeDAO.findOfficeByPrimaryKey(office.getOfficecode());
}
这里是我已经发布了JSON对象:
{
"state":"CA",
"country":"USA",
"officecode":"20",
"city":"Vancouver",
"phone":"+1 650 219 4782",
"addressline1":"100 Market Street",
"addressline2":"Suite 300",
"postalcode":"94080",
"territory":"NA",
"employees":[
{
"extension":"x5800",
"employeenumber":2001,
"lastname":"joe",
"firstname":"joe",
"email":"[email protected]",
"reportsto":null,
"jobtitle":"President",
"pay":null,
"officecode":"20"
},
{
"extension":"x5800",
"employeenumber":2002,
"lastname":"mary",
"firstname":"mary",
"email":"[email protected]",
"reportsto":null,
"jobtitle":"Vice President",
"pay":null
"officecode":"20"
}
]
}
我更新了答案,请参阅以下如何填充从员工表到外部表的外键。 –
你有没有去过它? –