Hibernate exceptions are thrown because of special reasons. Each of these reasons needs a treatment of its own. For some of the exceptions, as ConstraintViolationException
, you even have to differentiate whether they are caused by errors from users or server-side bugs. If the user provided invalid input data you have to tell the user that the input should be corrected. If the server manipulated the data incorrectly you have to tell the client that something went wrong but it wasn't his/her fault.
What you can do, however, is catch HibernateException
in general in your servlet and act upon the actual exception type. Depending on the exception caught you should log enough information to reproduce the error and inform the user of the server-side failure accordingly (including what he/she should or can do to correct the cause).
In your example code, I would first search for possible errors in your code that cause the ConstraintViolationException
. If the user provided invalid input data, you can extract detailed information by calling getConstraintName()
of the ConstraintViolationException
instance. This extracted information should then be logged and presented to the user.