I’m just wondering if it is possible to make a save() with jpa(spring boot) to create an entity then a save to update inside a same transaction ? (we’re speaking about the same entity)
Because personally I got something like “optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)” I’m trying to fix this issue since 2 days but I found nothing over all forums, can you help me please ?
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:532)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:304)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
at com.valeo.foobar.credentialsmanager.controller.CredentialsManagerController$$EnhancerBySpringCGLIB$$4b0a1a07.createCertificateAuthority(<generated>)
It looks like the @Controller invokes the commit via the TransactionAspectSupport.commitTransactionAfterReturning which is very odd. Are you annotating the @Controller with @Transactional?
However, the only way to tell what’s wrong with your application is if you provide a replicating test case. Try building a test repository on GitHub which replicates the issue, and I will investigate it once it’s done.
About my initial question, what are limitations when a same entity is created then updated ?
I need a code example to understand what do you mean by creating, the modifying an entity. Is it in the same Tx? Is it in the same Persistence Context?
Because in my case in my code I’m doing:
CREATION of IPK
UPDATE of IPK
CREATION of CREDENTIALS
However during the flush the order is not the same:
CREATION of IPK
CREATION of CREDENTIALS
UPDATE of IPK
Can you explain me this behavior please ?
Check out this article for an explanation of the Hibernate flush order.
What do you mean by Tx ? Transaction ? yes it is in the same transaction
About the persistence context I don’t know, mainly because I’m using SpringBoot stack so it would be set by default for this option.