# Cascade and direct update within a single transaction on hibernate-6 throws ERRORs now

**URL:** <https://discourse.hibernate.org/t/cascade-and-direct-update-within-a-single-transaction-on-hibernate-6-throws-errors-now/10499>\
**Category:** Hibernate ORM\
**Created:** [October 22, 2024, 4:39pm UTC](https://discourse.hibernate.org/t/cascade-and-direct-update-within-a-single-transaction-on-hibernate-6-throws-errors-now/10499 "2024-10-22T16:39:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![huican](https://avatars.discourse-cdn.com/v4/letter/h/90db22/32.png) [@huican](https://discourse.hibernate.org/u/huican)\
**Post date:** [October 22, 2024, 4:39pm UTC](https://discourse.hibernate.org/t/cascade-and-direct-update-within-a-single-transaction-on-hibernate-6-throws-errors-now/10499/1 "2024-10-22T16:39:49Z")

</div>

Hi,  
I am new to this community 👋.  
After we migrate from hibernate 5 to 6, now we got some errors for Cascade delete and direct delete in a single transaction.

Is this error expected now on hibernate-6? On hibernate-5, it didn’t give ERROR.  
Any recommended fixes for this issue?

The Entity “deployment” has this bag:

```auto
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "deploymentId", orphanRemoval = true, cascade = CascadeType.ALL)
    private Set<ApplicationConfiguration> applicationConfigurations = new HashSet<>();

```

ApplicationConfiguration is another Entity.

In one @Transactional method, we directly update on the ApplicationConfiguration entity, also cascade Delete the deployment, such as Pesudo code:

```auto
   remove_some_applicationConfiguration(deployment);
   delete_Deployment(deployment);

```

Now on hibernate-6 we got ERROR:  
`HibernateOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1 statement executed: delete from configuration where id=?`

Thanks

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [October 22, 2024, 5:22pm UTC](https://discourse.hibernate.org/t/cascade-and-direct-update-within-a-single-transaction-on-hibernate-6-throws-errors-now/10499/2 "2024-10-22T17:22:55Z")

</div>

I don’t know what `remove_some_applicationConfiguration` does exactly, but if it removes a `ApplicationConfiguration` by calling e.g. `session.remove(appConfig)` without also removing the object from the `Deployment#applicationConfigurations` set, then I can imagine that this exception is possible.

You have to ensure that both sides of a bidirectional association are always in the correct state.

The question you should ask yourself though, is whether you need to call `remove_some_applicationConfiguration` in the first place, since removing a `Deployment` will cascade delete anyway.

---

<div class="post-metadata">

**Author:** ![huican](https://avatars.discourse-cdn.com/v4/letter/h/90db22/32.png) [@huican](https://discourse.hibernate.org/u/huican)\
**Post date:** [October 22, 2024, 5:57pm UTC](https://discourse.hibernate.org/t/cascade-and-direct-update-within-a-single-transaction-on-hibernate-6-throws-errors-now/10499/3 "2024-10-22T17:57:41Z")

</div>

Thanks a lot for your reply.  
The error is from the old legacy code, we do think that configuration one is redundant, we plan to remove the redundant one.  
The thing puzzled us is why that was working without ERROR for hibernate-5, but shows up on hibernate-6.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [October 23, 2024, 2:03pm UTC](https://discourse.hibernate.org/t/cascade-and-direct-update-within-a-single-transaction-on-hibernate-6-throws-errors-now/10499/4 "2024-10-23T14:03:39Z")

</div>

Read the migration guides. You might run into this scenario: [hibernate-orm/migration-guide.adoc at 6.6 · hibernate/hibernate-orm · GitHub](https://github.com/hibernate/hibernate-orm/blob/6.6/migration-guide.adoc#merge-versioned-entity-when-row-is-deleted)

---

<div class="post-metadata">

**Author:** ![huican](https://avatars.discourse-cdn.com/v4/letter/h/90db22/32.png) [@huican](https://discourse.hibernate.org/u/huican)\
**Post date:** [October 23, 2024, 11:45pm UTC](https://discourse.hibernate.org/t/cascade-and-direct-update-within-a-single-transaction-on-hibernate-6-throws-errors-now/10499/5 "2024-10-23T23:45:51Z")

</div>

Thank you a lot for your help
