StandardEditor - can't discard all modified and marked for remove entities from DataContext

Hello Jmix team,

How to clear all modified and marked for remove entities from the DataContext without closing the screen.

We have an entity X with several checkboxes.

On every cnanged checkbox the entity X is added into getModifiedInstances() hash set (which is immutable) and the interesting part here is that when we check the set content before commit we saw that there are several THE SAME entities (entity X).

How to clear the data context without closing the screen? We tried with all provided methods and there is no result.

BR,
Denislav Kanchev

Hi Denislav,

There are evictModified() and clear() methods that should clear the DataContext from the modified/deleted or all tracked entitities respectively. Did you try them?

Hi Konstantin,

Yes, we tried both methods and the result was the same - there is still one entity into the set.

As I mentioned we have the following scenario.

One entity (Document) with for example four checkboxes (checkbox1, checkbox2 and checkbox3).

  • When we checked “checkbox1”, the getModifiedEntities set is populated with entity “Document”.
  • After that when we checked “checkbox2”, the getModifiedEntities set is updated by adding the same entity “Document” and now the size of the set is 2 (but the entities are the same).

After invoking clear() or evictModified() methods the result is the same - getModifiedEntities size is reduced to 1 but not all of the entities are cleared (Which maybe is a bug).

PS: We want not to use the evictModified() method because we want to track these entities.

BR,
Denislav Kanchev

Sounds very strange. A set cannot contain two equal elements.
Could you provide a test project with the data model and the screen?

Hi Konstantin,

We fixed the issue. Actually there is no problem with the set. The problem was into our entity because we were overridden the equals() and hashCode() methods. By using the default equals and hashCode methods the set was populated correctly.

Is there a way to clear the dataContext getRemoved() set and continue track the entities?

Thanks!

No, at the moment DataContext has no appropriate methods.
Created issue to add them: Direct access to modified and removed instances in DataContext · Issue #1348 · jmix-framework/jmix · GitHub

Meanwhile, you can provide your own implementation of DataContext with a suitable method as follows:

  1. Create a subclass of DataContextImpl, for example MyDataContextImpl.
  2. Create a subclass of DataComponents factory and return MyDataContextImpl instance from createDataContext() method.
  3. Define @Primary bean of your DataComponents type as described in Overriding Spring Beans.