Skip to content
Advertisement

How to retrieve SQL Error message with Hibernate

I have developed a small application accessing a PostgreSQL database with Hibernate. When I try to delete a record involved in a foreign key with another table, a javax.persistence.PersistenceException is caught with the message:

Could not execute statement

But I noticed in the console window that there is a more detailed message as follows:

13:13:49,222 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-3) ERROR: update or delete on table "AdminSite" violates foreign key constraint "FK_AdminUserSite" on table "AdminUserProfile"
  Detail: Key (STCode)=(HO        ) is still referenced from the table "AdminUserProfile".

How to retrieve this message which is more meaningful?

Advertisement

Answer

Hibernate wraps SQLExceptions thrown while interacting with the database in a JDBCException. In fact, Hibernate will attempt to convert the exception into a more meningful subclass of JDBCException. The underlying SQLException is always available via JDBCException.getCause().

Hibernate documentation specifies the above and is available under hibernate documentation.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement