Merge entity with @OneToMany attributes leading to javax.persistence.EntityNotFoundException: No row with the given identifier exists

Hello,

during a session.merge() I’m getting a javax.persistence.EntityNotFoundException: No row with the given identifier exists Exception.

The business process is as follows:

  • Client receives an object with it’s dependencies loaded by the server
  • Client changes the dependencies (OneToMany) and sends it to the server
  • Server tries to merge

I added the source code showing the problem here (~5kb)
https://www.magentacloud.de/lnk/MfmYjJOQ

The doWork method leads to a unique constraint exception due to a item_id and language being a unique constraint and Hibernate doing the inserts before the deletes on flush.

The doWork2 method leads to the aforementioned javax.persistence.EntityNotFoundException: No row with the given identifier exists.

One way I found to fix the problem in doWork2 is to remove the snapshots of the PersistantSet doing ((PersistentSet) item.getLabel()).setSnapshot(null, null, null);

The questions I have are now as follows:

  1. Is Hibernate meant to do the inserts before the deletes in method doWork ?
  2. Will removing the snapshots on the PersistantSet have any strange sideeffects?
  3. Is there a better way to implement the given business process?

Note: We recently upgraded Hibernate from 3.6.4 to 5.2.12 and I think the exceptions didn’t occure back in the old version

Hope you can help me out, kind regards
René