Hibernate/jpa giving “flush during cascade is dangerous”

Hi,

I am using hibernate version 5.4.16 .I am getting below exception in my application after which all other transaction is giving the same error.

Caused by: javax.persistence.PersistenceException: org.hibernate.HibernateException: Flush during cascade is dangerous
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
at org.hibernate.internal.ExceptionConverterImpl.convertCommitException(ExceptionConverterImpl.java:65)
… 9 more
Caused by: org.hibernate.HibernateException: Flush during cascade is dangerous
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1356)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:451)
at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3210)
at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2378)
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:447)

I have not configure any flush mode . I don’t call flush() in my code.

Give me the cause of error which is coming from hibernate and help me to fix.

Thank you.

Could you please share the full stacktrace. Are you using JTA or a resource local configuration? Are you maybe doing a EntityTransaction.commit or EntityTransaction.rollback in an entity listener?

We are using resource local configuration and getting below errors while doing EntityTransaction.commit.Please find the below stack trace of the error.

Error while committing the transaction
javax.persistence.RollbackException: Error while committing the transaction
at org.hibernate.internal.ExceptionConverterImpl.convertCommitException(ExceptionConverterImpl.java:81)
at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:104)
Caused by: javax.persistence.PersistenceException: org.hibernate.HibernateException: Flush during cascade is dangerous
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:181)
at org.hibernate.internal.ExceptionConverterImpl.convertCommitException(ExceptionConverterImpl.java:65)
… 9 more
Caused by: org.hibernate.HibernateException: Flush during cascade is dangerous
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1356)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:451)
at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3210)
at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2378)
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:447)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:183)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$300(JdbcResourceLocalTransactionCoordinatorImpl.java:40)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:281)
at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:101)

Please provide the full stacktrace, otherwise I can’t see where this call is originating from

Hi beikov,

I´m have the same occurs of exception. I will provide the stacktrace, but I´m searching something about this exception.
If is possible to you to help with this, please return.
I return to you soon

It is an old thread but I have just run into the same problem:

javax.persistence.PersistenceException: 
org.hibernate.HibernateException: Flush during cascade is dangerous
                 at
org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:147)
                 at
org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
                 at
org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162)
                 at
org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1403)
                 at
org.hibernate.collection.internal.AbstractPersistentCollection$3.doWork(AbstractPersistentCollection.java:321)
                 at
org.hibernate.collection.internal.AbstractPersistentCollection$3.doWork(AbstractPersistentCollection.java:314)
                 at
org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:249)
                 at
org.hibernate.collection.internal.AbstractPersistentCollection.readElementExistence(AbstractPersistentCollection.java:313)
                 at
org.hibernate.collection.internal.PersistentSet.add(PersistentSet.java:183)
                 at
org.hibernate.type.CollectionType.replaceElements(CollectionType.java:518)
                 at
org.hibernate.type.CollectionType.replace(CollectionType.java:663)
                 at
org.hibernate.type.TypeHelper.replace(TypeHelper.java:177)
                 at
org.hibernate.event.internal.DefaultMergeEventListener.copyValues(DefaultMergeEventListener.java:393)
                 at
org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:327)
                 at
org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:169)
                 at
org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:881)
                 at
org.hibernate.internal.SessionImpl.merge(SessionImpl.java:848)
                 at
org.hibernate.engine.spi.CascadingActions$6.cascade(CascadingActions.java:261)
                 at
org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:391)
                 at
org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:316)
                 at
org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:155)
                 at
org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:424)
                 at
org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:356)
                 at
org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:319)
                 at
org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:155)
                 at
org.hibernate.engine.internal.Cascade.cascade(Cascade.java:104)
                 at
org.hibernate.event.internal.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:460)
                 at
org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:326)
                 at
org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:169)
                 at
org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:881)
                 at
org.hibernate.internal.SessionImpl.merge(SessionImpl.java:848)
                 at
org.hibernate.engine.spi.CascadingActions$6.cascade(CascadingActions.java:261)
                 at
org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:391)
                 at
org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:316)
                 at
org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:155)
                 at
org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:424)
                 at
org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:356)
                 at
org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:319)
                 at
org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:155)
                 at
org.hibernate.engine.internal.Cascade.cascade(Cascade.java:104)
                 at
org.hibernate.event.internal.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:460)
                 at
org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:326)
                 at
org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:169)
                 at
org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:881)
                 at
org.hibernate.internal.SessionImpl.merge(SessionImpl.java:848)
                 at
org.hibernate.engine.spi.CascadingActions$6.cascade(CascadingActions.java:261)
                 at
org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:391)
                 at
org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:316)
                 at
org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:155)
                 at
org.hibernate.engine.internal.Cascade.cascade(Cascade.java:104)
                 at
org.hibernate.event.internal.DefaultMergeEventListener.cascadeOnMerge(DefaultMergeEventListener.java:460)
                 at
org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:326)
                 at
org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:169)
                 at
org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:68)
                 at
org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:857)
                 at
org.hibernate.internal.SessionImpl.merge(SessionImpl.java:838)
                 at
org.hibernate.internal.SessionImpl.merge(SessionImpl.java:843)
                 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.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:314)
                 at com.sun.proxy.$Proxy130.merge(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
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
                 at
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
                 at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
                 at
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367)
                 at
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118)
                 at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
                 at
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
                 at com.sun.proxy.$Proxy164.save(Unknown Source)
                 ...
                 at
org.aspectj.runtime.reflect.JoinPointImpl.proceed(JoinPointImpl.java:167)
                 ...
                 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:344)
                 at
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
                 at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
                 at
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367)
                 at
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118)
                 at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
                 at
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
                 at
com.sun.proxy.$Proxy385.doAbstractDecisionSave(Unknown Source)
                 ...
                 at
org.aspectj.runtime.reflect.JoinPointImpl.proceed(JoinPointImpl.java:167)
                 ...
                 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:344)
                 at
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
                 at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
                 at
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367)
                 at
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118)
                 at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
                 at
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
                 at com.sun.proxy.$Proxy478.transition(Unknown Source)
                 ...
                 at
org.aspectj.runtime.reflect.JoinPointImpl.proceed(JoinPointImpl.java:167)
                 ...
                 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:344)
                 at
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
                 at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
                 at
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367)
                 at
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118)
                 at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
                 at
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
                 at
com.sun.proxy.$Proxy208.doProcessStateTransition(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
org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
                 at
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
                 at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
                 ...
                 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:344)
                 at
org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
                 at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
                 at
org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:367)
                 at
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:118)
                 at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
                 at
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
                 at
com.sun.proxy.$Proxy146.executeTransactionallyThrowing(Unknown Source)
                 ...
                 at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
                 ...
                 at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
                 ...
                 at
org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
                 at
org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
                 at
com.sun.proxy.$Proxy956.doProcessStateTransition(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
org.springframework.remoting.support.RemoteInvocation.invoke(RemoteInvocation.java:215)
                 ...
                 at
org.springframework.remoting.support.DefaultRemoteInvocationExecutor.invoke(DefaultRemoteInvocationExecutor.java:39)
                 at
org.springframework.remoting.support.RemoteInvocationBasedExporter.invoke(RemoteInvocationBasedExporter.java:78)
                 at
org.springframework.remoting.rmi.RmiBasedExporter.invoke(RmiBasedExporter.java:73)
                 at
org.springframework.remoting.rmi.RmiInvocationWrapper.invoke(RmiInvocationWrapper.java:77)
                 at
sun.reflect.GeneratedMethodAccessor237.invoke(Unknown Source)
                 at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                 at java.lang.reflect.Method.invoke(Method.java:498)
                 at
sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)
                 at sun.rmi.transport.Transport$1.run(Transport.java:200)
                 at sun.rmi.transport.Transport$1.run(Transport.java:197)
                 at java.security.AccessController.doPrivileged(Native
Method)
                 at
sun.rmi.transport.Transport.serviceCall(Transport.java:196)
                 at
sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573)
                 at
sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:834)
                 at
sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688)
                 at java.security.AccessController.doPrivileged(Native
Method)
                 at
sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687)
                 at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
                 at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
                 at java.lang.Thread.run(Thread.java:748)
Caused by: org.hibernate.HibernateException: Flush during cascade is dangerous
                 at
org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1392)
                 ... 185 common frames omitted

Can you share the model and the persistence interaction? Ideally, you would provide a reproducing test case(hibernate-test-case-templates/JPAUnitTestCase.java at main · hibernate/hibernate-test-case-templates · GitHub) for this problem and then we can fix this.