master detail inline edit

I have created a master-detail UI and can insert new records, save in the detail table directly (inline edit). However, the master reference is not saved as i am using the datasource of detail. I am using the following codes in controller. Any help in correcting it to populate the master key in detail table


 @Inject
    private CollectionDatasource<ProductionGrLines, UUID> linesDs;

    @Inject
    private Metadata metadata;

    @Override
    public void init(Map<String, Object> params) {


    }
    

    public void onAdd(Component source) {
        linesDs.addItem(metadata.create(ProductionGrLines.class));
        
    }


    public void onRemove(Component source) {
        if (linesDs.getItem() != null) {
            linesDs.removeItem(linesDs.getItem());
        }
    }


    public void onSave() {
        if (validateAll()) {
            getDsContext().commit();
            showNotification(getMessage("changesSaved"), NotificationType.HUMANIZED);
        }
    }

    @Override
    public boolean validateAll() {
        for (ProductionGrLines task : linesDs.getItems()) {
            if (task.getArticle() == null ) {
                showNotification(getMessage("fillRequiredFields"), NotificationType.TRAY);
                return false;
            }
        }
        return super.validateAll();
    }

If you create detail items in your code, its your responsibility to set reference to a master entity. So you should write something like this:


public void onAdd(Component source) {
    ProductionGrLines line = metadata.create(ProductionGrLines.class);
    line.setMaster(masterDs.getItem());
    linesDs.addItem(line);
}

Thank you so much. It works perfectly.