父id不会插入java hibernate中的子表whit OneToMany关系

时间:2016-07-28 06:17:00

标签: java hibernate jpa

我有实体ServiceConfig与OneToMany关系:

    @OneToMany(mappedBy = "serviceConfig", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
    private List<NotificationItem> notificationItems;

    @OneToMany(mappedBy = "serviceConfig", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
    private List<QosItem> qosItems;

在每个儿童班,我都有:

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "TD_SERVICE_CONFIG_ID")
    private ServiceConfig serviceConfig;

当我获取新的ServiceConfig时 - 我有3个表,ServiceConfigNotificationItemQosItem。在NotificationItemQosItem表格中,我有空列TD_SERVICE_CONFIG_ID,但所有数据都已插入。

如何将hibernate(注释或其他配置)配置为成功插入父级id到TD_SERVICE_CONFIG_ID列的孩子?

1 个答案:

答案 0 :(得分:2)

您的配置看起来很好。

我怀疑(虽然显然无法在没有看到您的代码的情况下确定)您没有设置关系的反面,这是必需的。 您可以按如下方式添加NotificationItem

NotificationItem item = new NotificationItem();
item.setServiceConfig(service);  //set the other side of the bi-directional relationship
servive.getNotificationItems().add(item);

中间线是重要的一条。