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

Hi,
I am new to this community :wave:.
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:

    @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:

   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

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.

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.

Read the migration guides. You might run into this scenario: hibernate-orm/migration-guide.adoc at 6.6 · hibernate/hibernate-orm · GitHub

Thank you a lot for your help