Solved Hibernate ConstraintViolationException when trying to update data

Discussion in 'Spigot Plugin Development' started by robertlit, Jan 24, 2020.

  1. When using session.saveOrUpdate(Object) Hibernate throws a ConstraintViolationException saying that I can not insert a duplicate value, which is what I would expect if I were using But shouldn't the "update" part prevent this from happening and allow me to update an exisiting database entry?
    Is there another method for updating data with Hibernate?

    I have to mention that it only happens with MySQL, I have used Hibernate before with SQLite and it didn't happen.
    #1 robertlit, Jan 24, 2020
    Last edited: Jan 24, 2020
  2. Iwitrag


    It depends on the constraints (primary and foreign key)...
    You need to post more code and DB structure.

    I have tried it and it works correctly - but it's important to make sure that you do not violate the constrains - for example by providing object with id set to null (in this case Hibernate will try to insert it as new item).
  3. Forgot to mark this old thread as solved..
    I fixed it using the @SqlInsert annotation.