7

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.

Saram
  • 1,500
  • 1
  • 18
  • 35
  • Have you look at this post(https://forum.hibernate.org/viewtopic.php?p=2414824)? – Yugang Zhou Jul 30 '13 at 09:44
  • It looks like your issue is similar to http://stackoverflow.com/questions/2995042/how-to-extract-actual-entity-and-property-name-that-is-a-duplicate-from-hibernat?rq=1. If yo want to do it. It will be based on database driver. – Haim Raman Jul 30 '13 at 12:38
  • I have seen what @Hippoom linked before, but I took it as outdated state of knowledge :) It look like 'sculpting stone by teeth'. Also I've seen what Haim pointed, but it is no solution... and also outdated. I believe it is possible, It's very common problem i think. – Saram Jul 30 '13 at 14:02

1 Answers1

3

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

Angga
  • 2,305
  • 1
  • 17
  • 21
  • 2
    That is the only solution I figured out. But for me it is not implementation independent. The message is different for all databases i know. I can not also believe that database vendor will not change it in future :( – Saram Jul 31 '13 at 09:13