Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract field name causing ConstraintViolationException

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.

like image 548
Saram Avatar asked Jul 26 '13 14:07

Saram


1 Answers

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).

like image 126
Angga Avatar answered Nov 23 '22 16:11

Angga