我想通過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"
}
]
}
我更新了答案,請參閱以下如何填充從員工表到外部表的外鍵。 –
你有沒有去過它? –