Reputation: 621
I am trying to save in data base nested objects using post request.
I have OneToMany relation. One Auto service -> many services.
Dependency: AutoService:
@JsonManagedReference
@OneToMany(mappedBy = "autoService", fetch = FetchType.EAGER)
private List<Service> services = new ArrayList<Service>();
Service:
@JsonBackReference
@ManyToOne
@JoinColumn(name = "autoServiceId", nullable = false)
private AutoService autoService;
Here is the usage of Repository class:
public AutoService save(AutoService service) {
return repository.saveAndFlush(service);
}
Here is POST request:
@RequestMapping(value = "/saveService", method = RequestMethod.POST)
@ResponseBody public AutoService saveAutoService(@RequestBody AutoService autoService){
return dataBaseService.save(autoService);
}
As you can see i am get an object:
But then in console I am getting next message:
Hibernate: insert into AutoRate (imageURL, mapCoordinate, phoneNumber, serviceName, websiteURL, id) values (?, ?, ?, ?, ?, ?)
For some reeason hibernate don't save nested object - Service
.
Can any body help me with this ?
Upvotes: 0
Views: 3494
Reputation: 95
Add to @OneToMany(mappedBy = "autoService", fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
Upvotes: 1