添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I'm using PostgreSQL on production and H2 on development.

I handle DataIntegrityViolationException in a SpringBoot ControllerAdvice and I want to get the name of the constraint that drives to this exception.

My table is defined like that :

@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 !