PSQL exception when create entity with attached entity attribute

Hello everybody!
In my project I have four entities: brand, model, bank, bankValue;
Entity Bank has attribute value, at which attached List of Bank Values an
When I create new bank instance, and then when I from bank-edit create more than 1 new bank-value instance it throws psql-exception and allow to commit changes.

com.haulmont.cuba.core.global.RemoteException:
---
javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.3.7-cuba): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ОШИБКА: повторяющееся значение ключа нарушает ограничение уникальности "testone_bank_pkey"
 Подробности: Ключ "(id)=(39733b78-7362-8629-574e-15f43f5ac28e)" уже существует.
Error Code: 0
Call: INSERT INTO TESTONE_BANK (ID, CREATE_TS, CREATED_BY, DELETE_TS, DELETED_BY, UPDATE_TS, UPDATED_BY, VERSION, BRAND_ID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
   bind => [39733b78-7362-8629-574e-15f43f5ac28e, 2020-10-16 12:31:02.484, admin, null, null, 2020-10-16 12:31:02.484, null, 1, 6970fd58-7289-d114-5b8b-952b899c97db]
Query: InsertObjectQuery(com.company.testone.entity.Bank-39733b78-7362-8629-574e-15f43f5ac28e [new,managed])
---
org.eclipse.persistence.exceptions.DatabaseException: 
Internal Exception: org.postgresql.util.PSQLException: ОШИБКА: повторяющееся значение ключа нарушает ограничение уникальности "testone_bank_pkey"
 Подробности: Ключ "(id)=(39733b78-7362-8629-574e-15f43f5ac28e)" уже существует.
Error Code: 0
Call: INSERT INTO TESTONE_BANK (ID, CREATE_TS, CREATED_BY, DELETE_TS, DELETED_BY, UPDATE_TS, UPDATED_BY, VERSION, BRAND_ID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
   bind => [39733b78-7362-8629-574e-15f43f5ac28e, 2020-10-16 12:31:02.484, admin, null, null, 2020-10-16 12:31:02.484, null, 1, 6970fd58-7289-d114-5b8b-952b899c97db]
Query: InsertObjectQuery(com.company.testone.entity.Bank-39733b78-7362-8629-574e-15f43f5ac28e [new])
---
org.postgresql.util.PSQLException: ОШИБКА: повторяющееся значение ключа нарушает ограничение уникальности "testone_bank_pkey"
 Подробности: Ключ "(id)=(39733b78-7362-8629-574e-15f43f5ac28e)" уже существует.
   at com.haulmont.cuba.core.sys.ServiceInterceptor.aroundInvoke(ServiceInterceptor.java:96)
   at sun.reflect.GeneratedMethodAccessor118.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:644)
   at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633)
   at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175)
   at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
   at com.sun.proxy.$Proxy222.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:94)
   at com.haulmont.cuba.web.sys.remoting.LocalServiceProxy$LocalServiceInvocationHandler.invoke(LocalServiceProxy.java:159)
   at com.sun.proxy.$Proxy34.commit(Unknown Source)
   at com.haulmont.cuba.client.sys.DataManagerClientImpl.commit(DataManagerClientImpl.java:100)
   at com.haulmont.cuba.gui.model.impl.DataContextImpl.commitToDataManager(DataContextImpl.java:673)
   at com.haulmont.cuba.gui.model.impl.DataContextImpl.performCommit(DataContextImpl.java:651)
   at com.haulmont.cuba.gui.model.impl.DataContextImpl.commit(DataContextImpl.java:614)
   at com.haulmont.cuba.gui.screen.StandardEditor.lambda$commitChanges$4(StandardEditor.java:423)
   at com.haulmont.cuba.gui.screen.StandardEditor.commitChanges(StandardEditor.java:438)
   at com.haulmont.cuba.gui.screen.StandardEditor.closeWithCommit(StandardEditor.java:587)
   at com.haulmont.cuba.gui.screen.StandardEditor.commitAndClose(StandardEditor.java:553)
   at com.haulmont.bali.events.EventHub.publish(EventHub.java:170)
   at com.haulmont.cuba.gui.components.actions.BaseAction.actionPerform(BaseAction.java:222)
   at com.haulmont.cuba.web.gui.components.WebButton.buttonClicked(WebButton.java:67)
   at com.haulmont.cuba.web.widgets.CubaButton.fireClick(CubaButton.java:76)
   at com.vaadin.ui.Button$1.click(Button.java:57)
   at sun.reflect.GeneratedMethodAccessor148.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:498)
   at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:153)
   at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:115)
   at com.vaadin.server.communication.ServerRpcHandler.handleInvocation(ServerRpcHandler.java:431)
   at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:396)
   at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:260)
   at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:82)
   at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40)
   at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1578)
   at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:425)
   at com.haulmont.cuba.web.sys.CubaApplicationServlet.serviceAppRequest(CubaApplicationServlet.java:329)
   at com.haulmont.cuba.web.sys.CubaApplicationServlet.service(CubaApplicationServlet.java:215)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
   at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
   at org.springframework.web.filter.CompositeFilter$VirtualFilterChain.doFilter(CompositeFilter.java:108)
   at org.springframework.web.filter.CompositeFilter.doFilter(CompositeFilter.java:74)
   at com.haulmont.cuba.web.sys.CubaHttpFilter.doFilter(CubaHttpFilter.java:93)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
   at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
   at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
   at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
   at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
   at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1579)
   at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
   at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
   at java.lang.Thread.run(Thread.java:748)

Could you tell me what the problem? Maybe I built wrong a date-model?
I will be grateful for every aswer

Here my test project : tesProj.zip (86.1 KB)

and I have custom Repo-storage, if you will have any problems with building project - change gradle-wrapper.properties and at build gradle change ip-address on this - 185.156.1.16

Hi.
In Bank entity change value attribute type to Composition. Check our guide about associations and compositions for explanation.

Regards,
Natalia

Hi
Thank you for your advice it help to solve this exception
But I have modified entity BankValues and add dynamic attributes there.
Also I write method createValues which create Bank Values.
For the first time it works ok, but if we open for second time - we can duplicated values and when we try to edit them it throws exception:

java.lang.IllegalStateException: Dynamic attributes should be loaded explicitly
	at com.google.common.base.Preconditions.checkState(Preconditions.java:508)
	at com.haulmont.cuba.core.entity.BaseGenericIdEntity.getValue(BaseGenericIdEntity.java:138)
	at com.haulmont.chile.core.model.utils.InstanceUtils.getValueEx(InstanceUtils.java:134)
	at com.haulmont.chile.core.model.impl.AbstractInstance.getValueEx(AbstractInstance.java:172)
	at com.haulmont.cuba.gui.components.data.value.ContainerValueSource.getValue(ContainerValueSource.java:161)
	at com.haulmont.cuba.gui.components.data.value.ValueBinder$ValueBindingImpl.activate(ValueBinder.java:221)
	at com.haulmont.cuba.web.gui.components.WebAbstractValueComponent.setValueSource(WebAbstractValueComponent.java:54)
	at com.haulmont.cuba.gui.components.factories.AbstractComponentGenerationStrategy.setValueSource(AbstractComponentGenerationStrategy.java:493)
	at com.haulmont.cuba.gui.components.factories.AbstractComponentGenerationStrategy.createStringField(AbstractComponentGenerationStrategy.java:244)
	at com.haulmont.cuba.gui.components.factories.AbstractComponentGenerationStrategy.createDatatypeField(AbstractComponentGenerationStrategy.java:149)
	at com.haulmont.cuba.gui.components.factories.AbstractComponentGenerationStrategy.createComponentInternal(AbstractComponentGenerationStrategy.java:83)
	at com.haulmont.cuba.gui.components.factories.DefaultComponentGenerationStrategy.createComponent(DefaultComponentGenerationStrategy.java:52)
	at com.haulmont.cuba.gui.components.UiComponentsGenerator.generate(UiComponentsGenerator.java:51)
	at com.haulmont.cuba.web.gui.components.dynamicattributes.DynamicAttributesPanel.generateFieldComponent(DynamicAttributesPanel.java:217)
	at com.haulmont.cuba.web.gui.components.dynamicattributes.DynamicAttributesPanel.initPropertiesForm(DynamicAttributesPanel.java:113)
	at com.haulmont.cuba.web.gui.components.dynamicattributes.DynamicAttributesPanel.lambda$onCreate$0(DynamicAttributesPanel.java:99)
	at com.haulmont.bali.events.EventHub.publish(EventHub.java:170)
	at com.haulmont.cuba.web.gui.components.WebAbstractComponent.publish(WebAbstractComponent.java:100)
	at com.haulmont.cuba.web.gui.components.WebAbstractValueComponent.setValue(WebAbstractValueComponent.java:102)
	at com.haulmont.cuba.gui.components.data.value.ValueBinder$ValueBindingImpl.valueSourceStateChanged(ValueBinder.java:291)
	at com.haulmont.bali.events.EventHub.publish(EventHub.java:170)
	at com.haulmont.cuba.gui.components.data.value.ContainerValueSource.setState(ContainerValueSource.java:215)
	at com.haulmont.cuba.gui.components.data.value.ContainerValueSource.containerItemChanged(ContainerValueSource.java:222)
	at com.haulmont.bali.events.EventHub.publish(EventHub.java:170)
	at com.haulmont.cuba.gui.model.impl.InstanceContainerImpl.fireItemChanged(InstanceContainerImpl.java:163)
	at com.haulmont.cuba.gui.model.impl.InstanceContainerImpl.setItem(InstanceContainerImpl.java:94)
	at com.haulmont.cuba.gui.model.impl.InstanceLoaderImpl.load(InstanceLoaderImpl.java:117)
	at com.haulmont.cuba.gui.model.impl.ScreenDataImpl.loadAll(ScreenDataImpl.java:80)
	at com.haulmont.cuba.gui.model.impl.AnnotationBasedLoaderBeforeShow.loadData(AnnotationBasedLoaderBeforeShow.java:36)
	at com.haulmont.cuba.web.sys.WebScreens.loadDataBeforeShow(WebScreens.java:546)
	at com.haulmont.cuba.web.sys.WebScreens.show(WebScreens.java:434)
	at com.haulmont.cuba.gui.screen.Screen.show(Screen.java:309)
	at com.haulmont.cuba.gui.actions.list.EditAction.execute(EditAction.java:349)
	at com.haulmont.cuba.gui.actions.list.EditAction.actionPerform(EditAction.java:297)
	at com.haulmont.cuba.web.gui.components.WebAbstractTable.handleClickAction(WebAbstractTable.java:1295)
	at com.haulmont.cuba.web.gui.components.WebAbstractTable.lambda$initComponent$db766529$1(WebAbstractTable.java:1048)
	at jdk.internal.reflect.GeneratedMethodAccessor132.invoke(Unknown Source)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:567)
	at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:496)
	at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:273)
	at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:237)
	at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:1041)
	at com.vaadin.v7.ui.Table.handleClickEvent(Table.java:3126)
	at com.vaadin.v7.ui.Table.changeVariables(Table.java:2918)
	at com.haulmont.cuba.web.widgets.CubaTable.changeVariables(CubaTable.java:366)
	at com.vaadin.server.communication.ServerRpcHandler.changeVariables(ServerRpcHandler.java:611)
	at com.vaadin.server.communication.ServerRpcHandler.handleInvocation(ServerRpcHandler.java:457)
	at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:400)
	at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:260)
	at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:82)
	at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40)
	at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1578)
	at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:425)
	at com.haulmont.cuba.web.sys.CubaApplicationServlet.serviceAppRequest(CubaApplicationServlet.java:329)
	at com.haulmont.cuba.web.sys.CubaApplicationServlet.service(CubaApplicationServlet.java:215)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.web.filter.CompositeFilter$VirtualFilterChain.doFilter(CompositeFilter.java:108)
	at org.springframework.web.filter.CompositeFilter.doFilter(CompositeFilter.java:74)
	at com.haulmont.cuba.web.sys.CubaHttpFilter.doFilter(CubaHttpFilter.java:93)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1579)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.base/java.lang.Thread.run(Thread.java:835)

Could you tell what can I do wrong?
And will be very gratefully

Here the updated test project: application.zip (88.4 KB)

Just enable dynamic attributes loading in your data loader similar to this:

            <loader id="speakersDl" dynamicAttributes="true">
                <query>
                    <![CDATA[select e from kubetest_Speaker e]]>
                </query>
            </loader>

1 Like

Hi, Andrey!
Unfortunately, it didn’t solved my problem it is still creates duplicates and throws exceptions when I try to edit more than one Value

Please read the documentation section about Dynamic attributes.

I do not understand why you add association with Category entity to your entity.

Do you mean why i use fields assosiated with Category at two entities?

1 Like

Yes.

Please add dynamic attributes to your entities according to documentation

Finally, I add them according documentation,
and removed Category from second Entity
But it didn’t solve my problems

Can I did something wrong with method createValues which is realised at BankEdit controller, but this doesn’t work when duplicates create

Here the my upgraded test - project: application2.zip (88.2 KB)