Hey guys,
I was hoping I can get some advice on how to handle the sql constraint violations.
For example, when I save a row in a table with multiple unique constraints, if I get an error for one of the values how can I determine which constraint was violated?
I was hoping that I can somewhat get the constraint name but some of the db's dialects don't support this feature.
i.e. mysql, which I'm using right now, returns something like this:
Code:
WARN [SocketListener0-9] - JDBCExceptionReporter - SQL Error: 1062, SQLState: 23000
ERROR [SocketListener0-9] - JDBCExceptionReporter - Duplicate entry '777' for key 2
I cannot use any of the information available here because mysql may not be the database used in the final product.
Code:
try {
getSession().saveOrUpdate(entity);
} catch (ConstraintViolationException ce) {
// ce.getConstraintName() returns null
}
and the constraint name is not available in the exception raised.
One of the ideas was to pre-validate the values but I'm afraid this could be expensive as extra sql statements are executed.
Does anybody have a good strategy for reporting the sql constraint exceptions back to the end user?
Thanks,
Dragos