Persist inner entity before commiting parent

Hello, i’m working on migrating my 6.9 project to 7.1 and faced some problems.
I have two entities, let’s say: Message, MsgContent. Message has a composition o2o to content. Content consists of a single field text. I created a view for message, and generic screens. DataContainer of Message has an inner container of metaDc and contentDc. I set property="content.text" for <richTextArea> component, and expect it to save the content of RTA into text of content entity, but it never happens, as i hit OK button, contents is null, same as if I put into RTA the dataContainer="metaDc" property="text"… Can I declaratively make the screen commit inner entities before the main one? Even more, maybe i could set the default value of the content.text, and then commit it with the whole message?sample.zip (21.5 KB)

Hi,
In the MessageEdit screen controller you should explicitly initialize the new Content entity and merge it to the dataContext:

@UiController("innerdcsample_Message.edit")
@UiDescriptor("message-edit.xml")
@EditedEntityContainer("messageDc")
@LoadDataBeforeShow
public class MessageEdit extends StandardEditor<Message> {

    @Inject
    private Metadata metadata;

    @Inject
    private DataContext dataContext;

    @Subscribe
    public void onInitEntity(InitEntityEvent<Message> event) {
        Message message = event.getEntity();
        message.setContent(createContent());
    }

    private Content createContent() {
        Content content = metadata.create(Content.class);
        return dataContext.merge(content);
    }
}

See the One-to-One Composition with a Single Editor section of the Data Modelling: Composition guide for details.

1 Like

Thank you! It was not obvious)

There is also DataContext.create() method that creates a new instance and merges it in one go.

1 Like

is it correct, that this kind of explicit actions are needed only when i create a new item? anytime i enter this screen to edit - everything will be tied up in dataContainer?

This is related to DataContext, not data containers. And yes, everything loaded to the screen through the loaders will be tracked by the data context automatically. Only if you create some instances or load them directly by DataManager or your custom service, you have to merge them into DataContext if you want them to be saved automatically on screen commit.

1 Like