简体   繁体   中英

Parent id not insert to child table whit OneToMany relationship in java hibernate

I have entity ServiceConfig with to OneToMany relationship:

    @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;

And in each child class, I have:

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

When I fetch new ServiceConfig - I have 3 tables, ServiceConfig , NotificationItem and QosItem . In NotificationItem and QosItem tables, I have empty column TD_SERVICE_CONFIG_ID but all data inserted.

How can I config hibernate(annotations or another configs) for successful insert parent id to TD_SERVICE_CONFIG_ID column of child?

Your configuration looks fine.

I suspect (although obviously can't tell for sure without seeing your code) that you are not setting the inverse side of the relationship, which is required. You would add a NotificationItem as follows:

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

The middle line is the important one here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM