Persisting beans from editable Grid

I am new to Grids and currently I am trying to figure out how to safe a Bean of the Property I just edited. I have a grid with a BeanContainer where I am trying to add a CommitHandler which will call our internal service (according to
http://stackoverflow.com/a/37037592
):

[code]
grid = new Grid();
grid.removeAllColumns();
createColumnsMap().forEach((key, value) → {
grid.addColumn(key).setHeaderCaption(value);
});
grid.setEditorEnabled(true);

BeanContainer<String, SomeClass> container = new BeanContainer<>(SomeClass.class);
container.setBeanIdProperty(“id”);
container.addAll(service.loadAll());
grid.setContainerDataSource(container);

BeanFieldGroup fieldGroup = new BeanFieldGroup<>(SomeClass.class);
fieldGroup.addCommitHandler(new FieldGroup.CommitHandler() {
@Override
public void preCommit(FieldGroup.CommitEvent commitEvent) throws FieldGroup.CommitException {
System.out.println(“Pre commit: Do Nothing”);
}

@Override
public void postCommit(FieldGroup.CommitEvent commitEvent) throws FieldGroup.CommitException {
    System.out.println("Post commit: Call service.save(object)");
}

});
grid.setEditorFieldGroup(fieldGroup);
[/code]The Problem is that the code from postCommit() is not called after clicking save:


I am not sure if overwriting the postCommit of the CommitHandler is even the right approach. Is there a better way?

26314.png

I found out myself in the meantime, I had to set the ItemDataSource of the BeanFieldGroup and then it worked:

grid.addItemClickListener(event -> {
    String internalObjectId = (String) event.getItemId();
    InternalObject internalObject = service.loadInternalObject(internalObjectId);
    
    ComboBox internalObjectsComboBox = new ComboBox();
    BeanContainer<String, InternalObject> internalObjectsContainer = new BeanContainer<>(InternalObject.class);
    internalObjectsContainer.setBeanIdProperty("id");
    List<InternalObject> internalObjects = service.findInternalObjects();
    internalObjectsContainer.addAll(internalObjects);
    internalObjectsComboBox.setContainerDataSource(internalObjectsContainer);
    internalObjectsComboBox.setScrollToSelectedItem(true);
    internalObjectsComboBox.setItemCaptionPropertyId("name");

    BeanFieldGroup<InternalObject> fieldGroup = new BeanFieldGroup<>(InternalObject.class);
    fieldGroup.bind(internalObjectsComboBox, "internalId");
    
    fieldGroup.setItemDataSource(internalObject);
    
    fieldGroup.addCommitHandler(new FieldGroup.CommitHandler() {
        @Override
        public void preCommit(FieldGroup.CommitEvent commitEvent) throws FieldGroup.CommitException {
            // Do Nothing
        }

        @Override
        public void postCommit(FieldGroup.CommitEvent commitEvent) throws FieldGroup.CommitException {
            FieldGroup fieldBinder = commitEvent.getFieldBinder();
            String internalId = (String) fieldBinder.getField("internalId").getValue();
            if (!Strings.isNullOrEmpty(internalId)) {
                InternalObject loadedInternalObject = service.loadInternalObject(internalObjectId);
                loadedInternalObject.setInternalId(internalId);
                service.saveInternalObject(loadedInternalObject);
            }
        }
    });

    grid.setEditorFieldGroup(fieldGroup);
}


This seems really nasty though, since it reacts every time the grid is clicked which can be an expensive operation. Is there no better way?