When is entity merged in dataContext?

Hi,

I am trying to gain some understanding on the mechanics underlying WebXXXXFields and at which point upon value change/settlement the dataContext is affected, triggering a database update.

After examining the setValue() method in WebAbstractField, I presumed extending a, let’s say WebPickerField, and customizing it to work with custom StandardEntity’s, it would trigger the inner mechanism inside Cuba to track the entity and attach and merge it into the dataContext of a regular StandardEditor.

To test it I created a StandardEditor, with this custom field in it and edited its values. The setValue() method is called properly and changes are published, but when checking how it affects the dataContext I verify that it doesn’t.

So how can I, inside the field, trigger the dataContext to keep track of the modified/settled instances?

Thanks and regards.

Carlos.

Hi,

Please note that when you merge an entity to the data context, the clone of this entity is returned, and you should edit this clone instance. E.g. assume that you have personDataContainer of InstanceContainer type and you want to get person from extental service, edit it and save to the database. The code in your case will look like this:

//fetching person from external service, it is not merged yet 
Person p = personService.getPerson(); 
//attaching the entity to the context, returning clone with injected listeners that track changes
Person merged = dataContext.merge(p); 
//adding the entity to the data container, all bound fields will display the properties values
personDataContainer.setItem(merged);

After that, if you have your editor fields bound to the data container and properties, you will be able to edit the person entity, and all changes will be tracked.

Hope this answers your question.

1 Like