How can i catch this Exception :
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
Duplicate entry '22-85' for key 'ID_CONTACT'
How can i catch this Exception :
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
Duplicate entry '22-85' for key 'ID_CONTACT'
In my Spring project, the exception thrown was
org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.ConstraintViolationException: could not execute statement; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
.So after debugging I had a parent
JpaSystemException
with a causePersistenceException
and that had a causeConstraintViolationException
, this last one had the Database specific exception, but I ended up catchingConstraintViolationException
withPlease note that while this works. I suggest to solve the problem by issuing a findBy before the save, as this is messy, and I think it's not warranted that it will work in future versions, may even break without notification.
I use Spring. So catch org.springframework.dao.DuplicateKeyException
Loook at Spring framework source code Look at spring JDBC error resolve code.
There are multiple ways how you can hit different Exception translators:
So Dublicate behavior may change from DuplicateKeyException to DataIntegrityViolationException.