Request coding examples and guide for commitDelegate

Hi CUBA,

Is there any coding examples and guide for commitDelegate?

I can’t find from the documentation

@Install(target = Target.DATA_CONTEXT)
protected Set commitDelegate(CommitContext commitContext) {
return null;
}

Regards,
CK

Hi,

You can find commit delegate’s usage in this platform class: com.haulmont.cuba.gui.model.impl.DataContextImpl#commitToDataManager

        if (commitDelegate == null) {
            return getDataManager().commit(commitContext);
        } else {
            return commitDelegate.apply(commitContext);
        }

So, the minimal delegate implementation should look like below:

    @Inject
    private DataManager dataManager;

    @Install(target = Target.DATA_CONTEXT)
    private Set<Entity> commitDelegate(CommitContext context) {
        EntitySet committed = dataManager.commit(context);
        return committed;
    }

As you can imagine, in this method you can change some attributes of entities to be committed, or execute logic just before / after committing changes to the database, or replace the commit logic completely (e.g. for implementing editor for non-persistent entity).

1 Like