Trouble on saving altered data


Once again I find myself in trouble using nested objects and changes to them. Sorry to ask for your help once more but I am unable to explain what I’m doing wrong (or even what is going wrong). Please bare with me.

To allow for editing and creating objects a number of levels deep, I have created a workaround that saves the parent object (if newly created) before child objects are added. The code below shows the general construction on this (within the editor of the parent):

    private Datasource<Parent> parentDs;

    private CreateAction childCreateAction;

    private boolean firstSave;

    protected void postInit() {
        Parent p = parentDs.getItem();

        // If a required field is empty then this a new item
        if (p.getTitle() == null) {
            firstSave = true;
            // If new, then we do not allow childs to be added until the object is saved
            childCreateAction.setBeforeActionPerformedHandler(() -> {
                // Save the object before adding anything
                if (firstSave) {
                    firstSave = !super.commit();
                return !firstSave;

Doing this works fine and I can create objects multiple levels deep. However, when altering the data of the parent AFTER the child has been added (and thus the parent already saved), there is an error:

java.lang.ClassCastException: org.eclipse.persistence.indirection.IndirectSet cannot be cast to com.haulmont.cuba.core.entity.Entity
	at com.haulmont.cuba.gui.components.EditorWindowDelegate.commit(
	at com.haulmont.cuba.web.gui.WebWindow$Editor.commitAndClose(
	at com.haulmont.cuba.gui.components.AbstractEditor.commitAndClose(
	at com.axxemble.base27.web.risk.RiskEdit.onBtnSaveClick(
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(
	at java.lang.reflect.Method.invoke(
	at com.haulmont.cuba.gui.xml.DeclarativeAction.actionPerform(
	at com.haulmont.cuba.web.gui.components.WebButton.performAction(
	at com.haulmont.cuba.web.gui.components.WebButton.lambda$new$61446b05$1(
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(
	at java.lang.reflect.Method.invoke(
	at com.vaadin.event.ListenerMethod.receiveEvent(
	at com.vaadin.event.EventRouter.fireEvent(
	at com.vaadin.event.EventRouter.fireEvent(
	at com.vaadin.server.AbstractClientConnector.fireEvent(
	at com.vaadin.ui.Button.fireClick(
	at com.haulmont.cuba.web.toolkit.ui.CubaButton.fireClick(
	at com.vaadin.ui.Button$
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(
	at java.lang.reflect.Method.invoke(
	at com.vaadin.server.ServerRpcManager.applyInvocation(
	at com.vaadin.server.ServerRpcManager.applyInvocation(
	at com.vaadin.server.communication.ServerRpcHandler.handleInvocation(
	at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(
	at com.vaadin.server.communication.ServerRpcHandler.handleRpc(
	at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(
	at com.haulmont.cuba.web.sys.CubaVaadinServletService$CubaUidlRequestHandler.lambda$synchronizedHandleRequest$0(
	at com.haulmont.cuba.web.sys.CubaVaadinServletService.withUserSession(
	at com.haulmont.cuba.web.sys.CubaVaadinServletService$CubaUidlRequestHandler.synchronizedHandleRequest(
	at com.vaadin.server.SynchronizedRequestHandler.handleRequest(
	at com.vaadin.server.VaadinService.handleRequest(
	at com.vaadin.server.VaadinServlet.service(
	at com.haulmont.cuba.web.sys.CubaApplicationServlet.serviceAppRequest(
	at com.haulmont.cuba.web.sys.CubaApplicationServlet.service(
	at javax.servlet.http.HttpServlet.service(
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(
	at com.haulmont.cuba.web.sys.CubaHttpFilter.handleNotFiltered(
	at com.haulmont.cuba.web.sys.CubaHttpFilter.doFilter(
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(
	at org.apache.catalina.core.StandardWrapperValve.invoke(
	at org.apache.catalina.core.StandardContextValve.invoke(
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(
	at org.apache.catalina.core.StandardHostValve.invoke(
	at org.apache.catalina.valves.ErrorReportValve.invoke(
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(
	at org.apache.catalina.core.StandardEngineValve.invoke(
	at org.apache.catalina.connector.CoyoteAdapter.service(
	at org.apache.coyote.http11.Http11Processor.service(
	at org.apache.coyote.AbstractProcessorLight.process(
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(
	at java.util.concurrent.ThreadPoolExecutor.runWorker(
	at java.util.concurrent.ThreadPoolExecutor$
	at org.apache.tomcat.util.threads.TaskThread$

This error also occurs when doing this only one level deep (parent -> child).

On trial-and-error I found out that when replacing the default super.commitAndClose() with savind the data myself, I managed to avoid the error:

        // Do our own save to avoid issues on the way back
        CommitContext cc = new CommitContext().addInstanceToCommit(r);
        // And close (forcefully) - but this doesn't refresh the table on the browser!
        super.close(CLOSE_ACTION_ID, true);

However, in this case the initial browser window is not refreshed and newly created items are not shown. I know this can be overcome as well but it seems I’m on the wrong track here. It does however point out that somehow the default commit is “out-of-sync”?

I tried to set up a test project with this issue but somehow everything works fine in the test project. So, next I tried to strip the existing project to only have the essential code. In this way, I found out that when using only the above, the issue already occurs. Why this is different from the test project I have no idea.

The project has become rather big and I’ve not yet decided to make it open source :wink: so I am unable to attach it to this topic.

Any ideas what might be wrong here? I am hoping that the error (stacktrace) and description rings a bell to you…

Thanks in advance for your help.

Extra information: found out that even without using the save on newly created parents before adding children may introduce the error following this scenario:
1- create parent & save (without adding chidren)
2- reopen parent for editing and add a child (this does not cause the ‘first save’ to trigger)
3- after adding the child, alter parent data and save

same error occurs

Still no idea what causes this behavior but thought you may want to know. Meanwhile I’m still stripping the original project further to match the test project…

I think my main question would be what the error really indicates? Is there a mismatch in properties/objects/status?

Even more trial-and-error, and looking at the sources in the location where the error is occurring, it turns out that I dod not have to include all the datasource attributes in my dsContext.

This comment set me on the right track:

    // Replace the reference to master entity with actual entity containing in the master datasource,
    // because in case of nested property datasources there may be references to cloned master entities.
    protected void replaceMasterCopies(Entity entity, NestedDatasource datasource)  {

So after modifying the dsContext on the editors as shown below (commented out the unnecessary stuff) , the error disappeared:

        <datasource id="riskDs"
            <collectionDatasource id="riskFilesDs"
                <datasource id="riskSysFileDs"
            <collectionDatasource id="riskCmDs"
                <collectionDatasource id="cmCommentsDs"
                <collectionDatasource id="cmFilesDs"
                    <datasource id="cmSysFileDs"

                <collectionDatasource id="cmParticipantsDs"
                                <datasource id="cmOwnerDs"
                                <datasource id="cmInfoSystemDs"
                                <datasource id="cmRiskDs"
            <datasource id="riskOwnerDs"
            <datasource id="riskInfoSystemDs"
            <datasource id="riskRaDs"
        <collectionDatasource id="staffsDs"
                <![CDATA[select e from base$Staff e where = true]]>
        <collectionDatasource id="informationSystemsDs"
                <![CDATA[select e from base$InformationSystem e where e.status < 5]]>

I consider my issue solved.

Hi Berend,

I would appreciate if you could still reproduce the issue in a test project. My own attempts to do it failed, but I would like to fix the code for all possible use cases.

Hi Konstantin,

I am happy to help out. Attached is a project that will generate the same issue as I have experienced. It may have a little bit more than really necessary but at least it’s reproducible.

When running the application:

  • create a meeting and set the title
  • add a topic and set the title for this topic
  • add a comment to this topic and set the comment text
  • save comment (confirm to save on dialog)
  • set related value on topic (e.g. “1”)
  • save topic
  • set related value on meeting (e.g. “1”)
  • save topic

error occurs

Hope this helps. (207.1K)

Thank you Berend,

I’ve fixed the issue for your case. The cause was in declaring a datasource for the reference to the master entity.

:ticket: See the following issue in our bug tracker: