Relationship that was not marked cascade PERSIST - New Entity linked to Existing Entity

I am building a system that has many entities, but specifically, the entities that I am having trouble with are:

A Patient has many Addresses and the Addresses belong to a State. I am attempting to create a new Address and select a State from a dropdown, but when I do and attempt to save I get: IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: com.rx30.ems.basicworkflowmanager.entity.State-97894f81-9789-4f81-d655-5505d6555505 [detached].

I attempted to mark the State relationship as CascadeType.ALL, even though I read previous posts that said this could be bad, and when I do I get the error stating that it couldn’t fetch the unfetched property [createdBy]. I can’t seem to figure out what is going on, and I have beat myself pretty badly for the last couple of hours trying to figure it out.

Address Entity:

    @OnDeleteInverse(DeletePolicy.UNLINK)
    @OnDelete(DeletePolicy.UNLINK)
    @Lookup(type = LookupType.DROPDOWN, actions = {"lookup", "open", "clear"})
    @NotNull(message = "{msg://com.rx30.ems.basicworkflowmanager.entity/Common.NotNull}")
    @ManyToOne(fetch = FetchType.LAZY, optional = false, cascade = CascadeType.MERGE)
    @JoinColumn(name = "STATE_ID")
    protected State state;

State Entity:

    @Composition
    @OnDeleteInverse(DeletePolicy.UNLINK)
    @OnDelete(DeletePolicy.UNLINK)
    @OneToMany(mappedBy = "state")
    protected Set<Address> addresses;

Address Create screen:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<window xmlns="http://schemas.haulmont.com/cuba/window.xsd"
        caption="msg://editorCaption"
        class="com.rx30.ems.basicworkflowmanager.web.address.AddressEdit"
        datasource="addressDs"
        focusComponent="fieldGroup"
        messagesPack="com.rx30.ems.basicworkflowmanager.web.address">
    <dsContext>
        <datasource id="addressDs"
                    class="com.rx30.ems.basicworkflowmanager.entity.Address"
                    loadDynamicAttributes="true"
                    view="patient-address-view">
            <datasource id="stateDs"
                        property="state"/>
            <datasource id="patientDs"
                        property="patient"/>
        </datasource>
        <collectionDatasource id="patientsDs"
                              class="com.rx30.ems.basicworkflowmanager.entity.Patient">
            <query>
                <!&#91;CDATA&#91;select e from basicworkflowmanager$Patient e&#93;&#93;>
            </query>
        </collectionDatasource>
        <collectionDatasource id="statesDs"
                              class="com.rx30.ems.basicworkflowmanager.entity.State">
            <query>
                <!&#91;CDATA&#91;select e from basicworkflowmanager$State e ORDER BY e.abbreviation&#93;&#93;>
            </query>
        </collectionDatasource>
    </dsContext>
    <dialogMode forceDialog="true"
                height="AUTO"
                modal="true"
                width="800"/>
    <layout expand="windowActions"
            spacing="true">
        <fieldGroup id="fieldGroup"
                    datasource="addressDs"
                    width="100%">
            <column width="250px">
                <field optionsDatasource="patientsDs"
                       property="patient"/>
                <field property="addressType"/>
                <field property="isPrimary"/>
            </column>
            <column>
                <field property="addressLine1"/>
                <field property="addressLine2"/>
                <field property="city"/>
                <field optionsDatasource="statesDs"
                       property="state"/>
                <field property="zipCode"/>
            </column>
        </fieldGroup>
        <frame id="windowActions"
               screen="editWindowActions"/>
    </layout>
</window>

If anyone can help, I would be grateful.

Hi Vincent,

It’s hard to say why the error occurs in this particular case without debugging the app, but I would suggest first to clean up your screen and entity from unneeded elements:

  • Remove ‘cascade’ definition from @ManyToOne

  • Remove ‘stateDs’ and ‘patientDs’ datasources from the screen - I don’t see any reason to have them, because there are no visual components linked to them.

Ok, so I have done that and it is still not working. The unneeded elements came out of me trying to follow some of the other guidance I found in other support posts about this problem.
Here is the full stack trace:


java.lang.IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: com.rx30.ems.basicworkflowmanager.entity.State-8d1f4efc-8d1f-4efc-6088-af386088af38 &#91;detached&#93;. 
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.discoverUnregisteredNewObjects(RepeatableWriteUnitOfWork.java:313) 
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.calculateChanges(UnitOfWorkImpl.java:728) 
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitToDatabaseWithChangeSet(UnitOfWorkImpl.java:1521) 
at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.commitRootUnitOfWork(RepeatableWriteUnitOfWork.java:278) 
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitAndResume(UnitOfWorkImpl.java:1174) 
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:134) 
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517) 
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761) 
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730) 
at com.haulmont.cuba.core.sys.TransactionImpl.commit(TransactionImpl.java:99) 
at com.haulmont.cuba.core.app.RdbmsStore.commit(RdbmsStore.java:392) 
at com.haulmont.cuba.core.app.DataManagerBean.commit(DataManagerBean.java:179) 
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.app.DataManagerBean$SecureDataManagerInvocationHandler.invoke(DataManagerBean.java:356) 
at com.sun.proxy.$Proxy338.commit(Unknown Source) 
at com.haulmont.cuba.core.app.DataServiceBean.commit(DataServiceBean.java:40) 
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 org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) 
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85) 
at com.haulmont.cuba.core.sys.ServiceInterceptor.aroundInvoke(ServiceInterceptor.java:115) 
at sun.reflect.GeneratedMethodAccessor140.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.$Proxy200.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:148) 
at com.sun.proxy.$Proxy30.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:166) 
at com.haulmont.cuba.gui.components.EditorWindowDelegate.commit(EditorWindowDelegate.java:281) 
at com.haulmont.cuba.web.gui.WebWindow$Editor.commitAndClose(WebWindow.java:1595) 
at com.haulmont.cuba.gui.components.AbstractEditor.commitAndClose(AbstractEditor.java:109) 
at com.haulmont.cuba.gui.components.EditorWindowDelegate$2.actionPerform(EditorWindowDelegate.java:101) 
at com.haulmont.cuba.web.gui.components.WebButton.performAction(WebButton.java:44) 
at com.haulmont.cuba.web.gui.components.WebButton.lambda$new$61446b05$1(WebButton.java:36)

Interestingly enough, there is another relationship in this same entity and it doesn’t seem to give this same error. I am not sure why this one is having a problem.

If your State entity is Versioned (or extended from StandardEntity), make sure you set the version column to a non-null value when importing.

So, I think I solved the problem and it wasn’t really the relationships. I had built a database script to import States into the database, and apparently, that was the problem. If I created a state through the interface, then it saved correctly. Can you help me understand the best way to setup ‘seed’ data that will be common across all platforms?

I’m sorry but I’m giving myself the same problem. The only difference I made was to change the database from a localized server to one on my PC.


IllegalStateException: During synchronization a new object was found through a relationship that was not marked cascade PERSIST: com.xxx.app.xxx.entity.configuration.Pais-0 [detached].

Change the HSQL data engine back to SQL Server and everything started working correctly

Hi Nelson,

Your problem is absolutely different and caused by the fact that HSQL cannot work with entities that have ID=0. In the latest version of Studio we changed the sequence generation statement to start from 1, but previously each first instance became unusable. This problem exists only for HSQL and for entities with IDENTITY primary keys.