I am trying to save 2 objects via RESTFUl service in a form which has nested relationship. I.e., an office has 2 employees.
However, as you can see from the following sample, without saving the office first, I won't be able to know the officecode as 20. Without:
"officecode":"20"
Although the json object is nested, however, I saved employees and offices OK, but they are not associated to each other.
How can I save nested objects in 1 submit then?
Entity looks like:
// 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;
}
Here is the rest call to save the office:
@RequestMapping(value = "/Office", method = RequestMethod.POST)
@ResponseBody
public Office newOffice(@RequestBody Office office) {
officeService.saveOffice(office);
return officeDAO.findOfficeByPrimaryKey(office.getOfficecode());
}
Here is the JSON object I have posted:
{
"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":"joe@classicmodelcars.com",
"reportsto":null,
"jobtitle":"President",
"pay":null,
"officecode":"20"
},
{
"extension":"x5800",
"employeenumber":2002,
"lastname":"mary",
"firstname":"mary",
"email":"mary@classicmodelcars.com",
"reportsto":null,
"jobtitle":"Vice President",
"pay":null
"officecode":"20"
}
]
}