@Table(name = "user", uniqueConstraints = {
@UniqueConstraint(name = "users_unique_email_idx", columnNames = {"email"})
class User ...
When I use PostgreSQL it works perfectly. I get the cause of exception which is a ConstraintViolationException
and then get the constraint name. Something like that :
((ConstraintViolationException)ex.getCause()).getConstraintName()
But using H2 Database I can't find a way to get the constraintName. The DataIntegrityViolationException
doesn't contain the ConstraintViolationException
or the constraintName. With H2 the error message is empty :
DataIntegrityViolationException:
could not execute statement; SQL [n/a]; constraint [null];
Caused by:
JdbcSQLException: Violation dindex unique ou clé primaire: {0}
Unique index or primary key violation: {0}; SQL statement:
Note : In my H2 INFORMATION_SCHEMA.CONSTRAINTS
table my constraint on email field exists and has the good name (users_unique_email_idx
)
Is it a limitation of H2 Database ?
How can I enable constraint name in H2 ?
Thanks !
Disclaimer :
I previously post this question on StackOverflow but I finally think that the question is interesting to share here because it concerns directly H2 database.
French messages were fixed in #1446, build H2 from its current sources or use another locale to check whether this bug is still reproducible. H2 should report name of the violated index (not the name of the constraint). Due to bug with French translation you got {0} instead of such name.
If Hibernate still cannot recover name of violated index or constraint even with proper exception message from H2, please discuss that with Hibernate community. There is not so much that we can do on our side. DataIntegrityViolationException
and ConstraintViolationException
are not parts of JDBC or H2, they are produced by Spring / Hibernate. JDBC does not provide a reliable way to report name of the index or constraint, so such name should be recovered by Hibernate from exception message somehow to construct its ConstraintViolationException
.
Perfect ! Thanks for the fix.
Waiting the release, is it possible to change the language of H2 to english to fix the problem manually ? (I'm using SprinBoot).
Thanks again !