Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConstraintViolationException VS DataIntegrityViolationException

I am writing a Spring app, and it seems that when I run into database errors, sometimes Hibernate's ConstraintViolationException is thrown and sometimes Spring's DataIntegrityViolationException is thrown. Is there a reason one might be thrown and not the other? Do they mean different things?

like image 565
Brandon Yarbrough Avatar asked Aug 07 '09 20:08

Brandon Yarbrough


1 Answers

If you execute Hibernate calls via the HibernateTemplate, then Spring will translate any Hibernate exceptions (e.g. ConstraintViolationException) into the Spring exception hierarchy (e.g. DataIntegrityViolationException). If you don't use HibernateTemplate, then the native Hibernate exceptions will go through. Both are RuntimeExceptions, so don't need explicit exception handling, so it's not always obvious which one you're going to see.

like image 66
skaffman Avatar answered Nov 22 '22 09:11

skaffman