# StaleObjectStateException 1 transaction 1 create and 1 update on the same entity

**URL:** <https://discourse.hibernate.org/t/staleobjectstateexception-1-transaction-1-create-and-1-update-on-the-same-entity/2021>\
**Category:** Hibernate ORM\
**Created:** [January 11, 2019, 4:03pm UTC](https://discourse.hibernate.org/t/staleobjectstateexception-1-transaction-1-create-and-1-update-on-the-same-entity/2021 "2019-01-11T16:03:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stanguc](https://avatars.discourse-cdn.com/v4/letter/s/6de8d8/32.png) [@stanguc](https://discourse.hibernate.org/u/stanguc)\
**Post date:** [January 11, 2019, 4:03pm UTC](https://discourse.hibernate.org/t/staleobjectstateexception-1-transaction-1-create-and-1-update-on-the-same-entity/2021/1 "2019-01-11T16:03:13Z")

</div>

Hi guys,

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 ?

Few logs: [https://gist.github.com/tanguc/032147b098c3799b3d833376a02c10a8](https://gist.github.com/tanguc/032147b098c3799b3d833376a02c10a8)

Thanks,

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [January 11, 2019, 4:57pm UTC](https://discourse.hibernate.org/t/staleobjectstateexception-1-transaction-1-create-and-1-update-on-the-same-entity/2021/2 "2019-01-11T16:57:57Z")

</div>

From your StackTrace:

```auto
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(&lt;generated&gt;)

```

It looks like the `@Controller` invokes the commit via the `TransactionAspectSupport.commitTransactionAfterReturning` which is very odd. Are you annotating the `@Controller` with `@Transactional`?

Also, Spring Boot uses the [Open-Session in View Anti-Pattern](https://vladmihalcea.com/the-open-session-in-view-anti-pattern/) by default. You might want to [disable it](https://stackoverflow.com/questions/30549489/what-is-this-spring-jpa-open-in-view-true-property-in-spring-boot/48222934).

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.

---

<div class="post-metadata">

**Author:** ![stanguc](https://avatars.discourse-cdn.com/v4/letter/s/6de8d8/32.png) [@stanguc](https://discourse.hibernate.org/u/stanguc)\
**Post date:** [January 11, 2019, 5:11pm UTC](https://discourse.hibernate.org/t/staleobjectstateexception-1-transaction-1-create-and-1-update-on-the-same-entity/2021/3 "2019-01-11T17:11:22Z")

</div>

I’m still getting the same error.

About my initial question, what are limitations when a same entity is created then updated ?

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 ?

Thanks

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [January 11, 2019, 5:33pm UTC](https://discourse.hibernate.org/t/staleobjectstateexception-1-transaction-1-create-and-1-update-on-the-same-entity/2021/4 "2019-01-11T17:33:58Z")

</div>

> 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](https://vladmihalcea.com/hibernate-facts-knowing-flush-operations-order-matters/) for an explanation of the Hibernate flush order.

---

<div class="post-metadata">

**Author:** ![stanguc](https://avatars.discourse-cdn.com/v4/letter/s/6de8d8/32.png) [@stanguc](https://discourse.hibernate.org/u/stanguc)\
**Post date:** [January 14, 2019, 10:10am UTC](https://discourse.hibernate.org/t/staleobjectstateexception-1-transaction-1-create-and-1-update-on-the-same-entity/2021/5 "2019-01-14T10:10:39Z")

</div>

Hi Vlad,  
thanks for your answer

Here are screens:  
First: ![main_entry](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex035/uploads/hibernate/original/1X/689a8a86c965d0c50e88dc0bc1a23f18c78598a0.png)

Second : [https://pasteboard.co/HWofGt9.png](https://pasteboard.co/HWofGt9.png)

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.

Thanks,

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [January 14, 2019, 11:41am UTC](https://discourse.hibernate.org/t/staleobjectstateexception-1-transaction-1-create-and-1-update-on-the-same-entity/2021/6 "2019-01-14T11:41:54Z")

</div>

Try to build a replicating test case that we can investigate. Otherwise, it’s impossible to tell what is causing the issue.
