Question about dataContext for compositions in edit screens

Hi all,
I am trying to understand how the dataContext works in greater detail. I read the documentation and I understand the concept. But I am unsure about automatic merging of entities in edit screens.
When I open an edit screen and debug into the dataContext I can see there are some entities in the context, how do these get added by Jmix? Is it the editedEntity + all direct compositions?

For example: I have an entity Company and that has a 1:1 composition of CEOInformation with a name and salary and also a 1:1 composition of an Address entity. The demands of our customers is not having to open popups for every sub-entity but to have a big edit screen with the fields of the Address all present.
So I made the screen with data components for all three entities and made forms and connected the fields to the properties. But the Address changes do not get saved because I saw that it is not in the dataContext. My thought is that compositions that go deeper than one level are not put there automatically?

How can I tackle this problem. I was thinking about merging all needed entities manually to the dataContext in “onBeforeShow”. But what else do I need to do? Put the resulting entity of the merge also in the edited entity like:
getEditedEntity().getCeoInformation.setAddress(dataContext.merge(getEditedEntity().getCeoInformation().getAddress())
And how does the dataComponent/loader notice that’s that the fields on the screen change the merged object?

My idea sounds a bit too complicated so I was thinking I am not getting the whole concept of the dataContext. In short: how to deal with compositions of more than one level of depth?

Thanks for any ideas! :blush:


Example of my data setup:

<data>
        <instance id="companyDc"
                  class="com.test.domain.Company">
            <fetchPlan extends="company-edit-fetch-plan"/>
            <loader/>
            <instance id="ceoInfoDc" property="ceoInfo">
                <instance id="addressDc" property="ceoAddress"/>
             </instance>
       </instance>
</data>

I think I MAYBE figured it out. I did not have the ceoAddress property in my fetch plan. After adding that, it is in the data context and also gets saved.
What threw me a bit off here was that I was inspecting the ceoInfo entity in the data context and there the address was loaded in that entity. I guess that is because lazy loading was happening when I used that property in the above data setup??
I don’t know if that is by design or gives any problems but maybe lazy loaded entities should be added to the data context of the screen?

Somehow some properties are still missing in the data context so it would be great to have some guide what needs to be done for a property to appear in the data context… maybe I am just missing something…

Indeed, I am too tired it seems. When creating a new entity and using dataContext.create(…) I forgot to set one property in the main entity so there was no id to link to the sub entity.

Exactly. Lazy loading ensures the related entity is loaded and set to the object graph, but it bypasses the DataContext.

Unfortunately we currently don’t have any idea of how to merge lazily loaded entities into the DataContext. These are too different mechanisms.

So all entities edited on the screen should be loaded eagerly by including references to them in the fetch plan.

1 Like

Thank you for the clarification. Including them in the fetch plan is a good idea anyway I think, so I know exactly what is really needed on a screen.