Hibernate不会插入嵌套对象

时间:2017-03-19 09:39:49

标签: java hibernate

我正在尝试使用post request保存在数据库嵌套对象中。

我有OneToMany关系。 One Auto服务 - >许多服务。

相关性: 汽车维修:

@JsonManagedReference
@OneToMany(mappedBy = "autoService", fetch = FetchType.EAGER)
private List<Service> services = new ArrayList<Service>();

服务:

@JsonBackReference
@ManyToOne
@JoinColumn(name = "autoServiceId", nullable = false)
private AutoService autoService;

以下是Repository类的用法:

public AutoService save(AutoService service) {
        return repository.saveAndFlush(service);
    }

这是POST请求:

@RequestMapping(value = "/saveService", method = RequestMethod.POST)
    @ResponseBody public AutoService saveAutoService(@RequestBody AutoService autoService){
        return dataBaseService.save(autoService);
    }

如你所见,我得到一个对象:

enter image description here

然后在控制台中,我收到了下一条消息:

Hibernate: insert into AutoRate (imageURL, mapCoordinate, phoneNumber, serviceName, websiteURL, id) values (?, ?, ?, ?, ?, ?)

对于某些reeason hibernate,不要保存嵌套对象 - Service

任何人都能帮助我吗?

1 个答案:

答案 0 :(得分:1)

添加到@OneToMany(mappedBy =“autoService”,fetch = FetchType.EAGER,cascade = CascadeType.PERSIST)