Add items to a nested collection container

I started to use nested data containers. That is pretty nice but doesn’t seam to update right if the nested container is a collection container.

I’ve build a screen with

<data>
    <instance id="orderDc"
              class="com.company.example.Order">
        <instance id="customerDc" property="customer"/>
        <collection id="lineItemsDc" property="lineItems"/>
    </instance>
</data>

When loading the screen this works fine and order, customer and line items are inside its containers. But when adding an item programmatically to orderDc e.g. with orderDc.getItem().getItems().add(dataManager.create(LineItem.class)) this is not inside lineItemsDc and does not appear on the screen. If I do it other way around with itemsDc.getMutableItems().add(dataManager.create(LineItem.class)) the new item is also in orderDc.

I wanted to use the orderDc to manage the different items on the screen and that works for the customer but not for the line items.

Is the idea wrong, to set a nested collection via parent entity?

Right, you should add new items directly to the collection container.
Also, don’t forget to set the inverse attribute and create new instance through DataContext if you want it to be saved automatically:

LineItem lineItem = dataContext.create(LineItem.class);
lineItem.setOrder(getEditedEntity());
itemsDc.getMutableItems().add(lineItem);