Editable table combined with edit action (details editor) runs into problems

Hi,

When using an editable table, having implemented an add button to directly start editing in the table and allowing for opening the editor itself, I’m running into problems when the item in the table is committed through the editor and then committed once more on saving the items in the table.

I am using a parent object that holds a list of associated items and I’m allowing to edit these items within the parents editor.

This is the dsContext in the parents editor:


<dsContext>
    <datasource id="parentDs"
                class="com.axxemble.base27.entity.Parent"
                view="parent-view">
        <collectionDatasource id="raRisksDs"
                              allowCommit="true"
                              property="risks">
    </datasource>
</dsContext>

This is my table xml in the parent editor:


<table id="risksTable"
       editable="true"
	   sortable="true"
	   height="100%">
	<actions>
		<action id="create"/>
		<action id="edit"/>
		<action id="remove"/>
	</actions>
	<columns>
		<column id="nr"/>
		<column id="title" editable="true"/>
		<column id="description" editable="true"
				collapsed="true"/>
		<column id="owner" editable="true"/>
		<column id="deadline" editable="true"/>
		<column id="riskLevel"/>
		<column id="status"/>
	</columns>
	<rows datasource="raRisksDs"/>
	<buttonsPanel width="100%">
			<button icon="font-icon:PLUS" caption="" invoke="addRisk"/>
			<button action="risksTable.edit"/>
			<button action="risksTable.remove"/>
	</buttonsPanel>
</table>

The addRisk function is as follows:


public void addRisk() {
    // Create risks and continue
    Parent ra = getItem();
    Risk r = metadata.create(Risk.class);
    r.setParent(ra);
    raRisksDs.addItem(r);
}

This works well when simply using the table for edits and then saving the parent. However, as the table allows for opening the item editor this runs into problems on newly created items as the editor commits the item and then when saving the parent, I get the error below:

Unique constraint violation: BASE_RISK_PKEY

Stack trace:


org.eclipse.persistence.exceptions.DatabaseException:
Internal Exception: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "base_risk_pkey"
  Detail: Key (id)=(e99d8725-8a81-25e7-3c7d-88ef9638359f) already exists.
Error Code: 0
Call: INSERT INTO BASE_RISK (ID, APPROACH, CREATE_TS, CREATED_BY, DEADLINE, DELETE_TS, DELETED_BY, DESCRIPTION, IMPACT, LIKELIHOOD, NR, RISK_FACTOR, RISK_LEVEL, STATUS, TITLE, UPDATE_TS, UPDATED_BY, VERSION, OWNER_ID, RELATED_SYSTEM_ID, RISK_ANALYSIS_ID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
        bind => &#91;e99d8725-8a81-25e7-3c7d-88ef9638359f, 0, 2017-06-06 16:19:56.577, admin, null, null, null, null, 3, 1, 25, 3, 0, 0, test, 2017-06-06 16:19:56.577, null, 1, 60885987-1b61-4247-94c7-dff348347f93, null, a3d06e94-b735-7107-d304-fe1e92fa0bff&#93;
Query: InsertObjectQuery(com.axxemble.base27.entity.Risk-e99d8725-8a81-25e7-3c7d-88ef9638359f &#91;detached&#93;)
---
org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "base_risk_pkey"
  Detail: Key (id)=(e99d8725-8a81-25e7-3c7d-88ef9638359f) already exists.
        at com.haulmont.cuba.core.sys.ServiceInterceptor.aroundInvoke(ServiceInterceptor.java:127)
        at sun.reflect.GeneratedMethodAccessor267.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:629)
        at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:618)
        at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:168)
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
        at com.sun.proxy.$Proxy214.commit(Unknown Source)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at com.haulmont.cuba.core.sys.remoting.LocalServiceInvokerImpl.invoke(LocalServiceInvokerImpl.java:95)
        at com.haulmont.cuba.web.sys.remoting.LocalServiceProxy$LocalServiceInvocationHandler.invoke(LocalServiceProxy.java:146)
        at com.sun.proxy.$Proxy25.commit(Unknown Source)
        at com.haulmont.cuba.client.sys.DataManagerClientImpl.commit(DataManagerClientImpl.java:96)
        at com.haulmont.cuba.gui.data.impl.GenericDataSupplier.commit(GenericDataSupplier.java:90)
        at com.haulmont.cuba.gui.data.impl.DsContextImpl.commit(DsContextImpl.java:164)
        at com.haulmont.cuba.gui.components.EditorWindowDelegate.commit(EditorWindowDelegate.java:266)
        at com.haulmont.cuba.web.gui.WebWindow$Editor.commitAndClose(WebWindow.java:1578)
        at com.haulmont.cuba.gui.components.AbstractEditor.commitAndClose(AbstractEditor.java:109)

Obviously, I would like to keep the edit action available in combination with the editable table.

Any help appreciated.

Hi Berend,

Your code would work well if the collection of Risks was a COMPOSITION. In this case the Risk editor would not commit to the database but to the parent datasource in the parent screen.

See an example project attached.

table-edit.zip (34.1K)