javahibernateexceptionconstraintviolationexception

How to prevent Hibernate from throwing ConstraintViolationException when using session.saveOrUpdate(Object)?


ConstraintViolationException is being thrown by hibernate when I use session.saveOrUpdate(Object). Shouldn't the "update" part prevent this excpetion from being thrown?

I have also tried using session.delete(Object) and then session.save(Object) but the same exception is thrown. How would I fix this?


Solution

  • Adding this annotation to the entity solved the issue:

    @SQLInsert(sql="REPLACE INTO table_name (column_1,column_2) VALUES (?,?)")