I need to show custom messages in my Spring 3.0 application. I have a database with Hibernate and there are several constraints. I have doubts in how DataIntegrityViolationException should be handled in a good way. I wonder if there is a way to map the exception with a message set in a properties file, as it is possible in Constraints validation. Could I handle it automatically in any way or I have to catch this exception in each controller?
I need to show custom messages in my Spring 3.0 application. I have a
Share
The problem with showing user-friendly messages in the case of constraint violation is that the constraint name is lost when Hibernate’s
ConstraintViolationExceptionis being translated into Spring’sDataIntegrityViolationException.However, you can customize this translation logic. If you use
LocalSessionFactoryBeanto access Hibernate, you can supply it with a customSQLExceptionTranslator(seeLocalSessionFactoryBean.jdbcExceptionTranslator). This exception translator can translate aConstraintViolationExceptioninto your own exception class, preserving the constraint name.