How can I get field name causing org.hibernate.exception.ConstraintViolationException
?
The only sure way to check unique constraint is transaction commit, so even if I check it before the exception can be thrown. So I need to communicate to user witch field causing save problem.
The detailed message is more or less technical and not acceptable by user. It also depends on database driver :(
IMO field name is enough, the problematic value I can get myself from object. Also other information I can prepare... but the field name.
can't you get the exception and the message by it's cause like this:
try{
t.commit();
}catch (ConstraintViolationException e) {
e.getCause().getMessage();//
}
that will gave you result like this [SQL0407] Null values not allowed in column or variable GROUP00002.
the last word is your column name, and you can translate it to match your field then(using static HashMap maybe).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With