Reputation: 27385
I'm used to check if the user try to store a valid data with cathcing the ConsratinViolationException
, like that:
try {
//persisitng to a db
}
catch (ConstraintViolationException e){
//Print message
}
I'm using PostgreSQL
and now I'm under the isssue that the persisitng can violate more than one different constarints. How can I distiguish in the catch clause what exactly constraint has been violated?
I need to do that, because the message the programm will print depending on that.
Upvotes: 1
Views: 880
Reputation: 26077
something like below
catch (ConstraintViolationException conEx) {
if (conEx.getConstraintName().contains("xyz_fK")) {
//TODO Project Entity is violating it's constrain
}
LOGGER.Info( "My log message", conEx.getConstraintName());
LOGGER.ERROR( "My log message", conEx);
Upvotes: 3
Reputation: 328
A ConstraintViolationException
contains a Set
of ConstraintViolation
s. Each of it contains information about a single violation. You can retrieve this Set
with getConstraintViolations()
and then iterate over it to find out, what has gone wrong in detail.
See http://docs.oracle.com/javaee/7/api/javax/validation/ConstraintViolationException.html.
Upvotes: 2