# StaleObjectStateException with LockType OPTIMISTIC\_FORCE\_INCREMENT and remove Entity

**URL:** <https://discourse.hibernate.org/t/staleobjectstateexception-with-locktype-optimistic-force-increment-and-remove-entity/3396>\
**Category:** Hibernate ORM\
**Created:** [October 29, 2019, 10:26am UTC](https://discourse.hibernate.org/t/staleobjectstateexception-with-locktype-optimistic-force-increment-and-remove-entity/3396 "2019-10-29T10:26:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![beickvon](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@beickvon](https://discourse.hibernate.org/u/beickvon)\
**Post date:** [October 29, 2019, 10:26am UTC](https://discourse.hibernate.org/t/staleobjectstateexception-with-locktype-optimistic-force-increment-and-remove-entity/3396/1 "2019-10-29T10:26:21Z")

</div>

Hi,

I’m using Hibernate 4.2.21 and I use OPTIMISTIC FORCE INCREMENT to update version of parent entities in 1-to-many relations if the children get updated. This works fine unless the parent is removed in the same transaction.  
Actually this can be reduced to a very simple example:

```auto
EntityManager em = ...
MyVersionedEntity entity = ...
em.lock (entity, LockModeType.OPTIMISTIC_FORCE_INCREMENT)
em.remove (entity)
em.getTransaction ().commit()

```

always gives the following exception:

```auto
Caused by: org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.syncrotess.gen.model.jpa.MyVersionedEntity#4621066252771655745]
	at org.hibernate.persister.entity.AbstractEntityPersister.forceVersionIncrement(AbstractEntityPersister.java:1834)
	at org.hibernate.action.internal.EntityIncrementVersionProcess.doBeforeTransactionCompletion(EntityIncrementVersionProcess.java:53)
	at org.hibernate.engine.spi.ActionQueue$BeforeTransactionCompletionProcessQueue.beforeTransactionCompletion(ActionQueue.java:748)
	at org.hibernate.engine.spi.ActionQueue.beforeTransactionCompletion(ActionQueue.java:338)
	at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:490)
	at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:105)
	at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
	at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:75)

```

The underlying issue is that the EntityIncrementVersionProcess trie to update the version though the entity is deleted. It works if I switch to PESSIMISTIC\_FORCE\_INCREMENT but that is hard to achieve in my concrete scenario, because at the point where I set the lock I cannot know if the entity is being removed later on and upgrading the OPTIMISTIC\_FORCE\_INCREMENT to a PESSIMISTIC\_FORCE\_INCREMENT does not work, i.e. Hibernate still tries to update the version on commit.

---

<div class="post-metadata">

**Author:** ![beickvon](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@beickvon](https://discourse.hibernate.org/u/beickvon)\
**Post date:** [October 29, 2019, 12:01pm UTC](https://discourse.hibernate.org/t/staleobjectstateexception-with-locktype-optimistic-force-increment-and-remove-entity/3396/2 "2019-10-29T12:01:15Z")

</div>

It is caused/fixed by [https://hibernate.atlassian.net/browse/HHH-13492](https://hibernate.atlassian.net/browse/HHH-13492)

---

<div class="post-metadata">

**Author:** ![beickvon](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@beickvon](https://discourse.hibernate.org/u/beickvon)\
**Post date:** [October 29, 2019, 12:54pm UTC](https://discourse.hibernate.org/t/staleobjectstateexception-with-locktype-optimistic-force-increment-and-remove-entity/3396/3 "2019-10-29T12:54:17Z")

</div>

@gbadner Is it safe to pick [https://hibernate.atlassian.net/browse/HHH-13492](https://hibernate.atlassian.net/browse/HHH-13492) into 4.2 branch? Is this branch still maintained or do I have to fork and build it myself?

---

<div class="post-metadata">

**Author:** ![gbadner](https://avatars.discourse-cdn.com/v4/letter/g/4bbf92/32.png) [@gbadner](https://discourse.hibernate.org/u/gbadner)\
**Post date:** [October 29, 2019, 4:55pm UTC](https://discourse.hibernate.org/t/staleobjectstateexception-with-locktype-optimistic-force-increment-and-remove-entity/3396/4 "2019-10-29T16:55:41Z")

</div>

Hibernate ORM 4.2 is no longer maintained. I believe it should would be OK to cherry-pick the fix back to 4.2, but I haven’t tried it myself so I can’t say for sure.

---

<div class="post-metadata">

**Author:** ![beickvon](https://avatars.discourse-cdn.com/v4/letter/b/c0e974/32.png) [@beickvon](https://discourse.hibernate.org/u/beickvon)\
**Post date:** [October 30, 2019, 9:58am UTC](https://discourse.hibernate.org/t/staleobjectstateexception-with-locktype-optimistic-force-increment-and-remove-entity/3396/5 "2019-10-30T09:58:34Z")

</div>

Thank you, I tried it and it worked.  
One question though, we have been using 4.2.21.FINAL because it has been the last 4.2.x release published to Maven Central. If I take a look at GitHub I find further release until 4.2.27.Final, where are those published?
