How to do an UPSERT with Hibernate and MySQL and avoid throwing "Duplicate entry for key" issues

Hi Vlad,

thank you very much for your answer, it really helped me to understand things a little bit more but still have a couple of questions.

  • Yes my code belongs to an ETL or batching process

  • My environment only has one client (which runs the ETL) and one server (web server/tomcat and database).

  • The client do calls to the server through a http request inside a simple loop

  • I am guessing I am not doing multi-threading yet my problem looks very similar.

  • Also I am using native queries to read if element already exists in the database (this should be accurate right?) but still persist fails due to a unique constraint violation.

  • I also tried nativequery instead of persist but the problem did not go away.

  • application managed entitymanager

I am just trying to understand a little bit deeper and I feel I still miss a few things

thank you very much