user3719630
user3719630

Reputation: 93

How to determinate hibernate to return sql error

I am trying to understand how it is possible to get sql error message from database during failed query through hibernate.

my database has a table like this:

 TABLE applications
 (
   id serial NOT NULL,
   name character varying(80) NOT NULL,
   CONSTRAINT applications_pkey PRIMARY KEY (id),
   CONSTRAINT applications_name_key UNIQUE (name)
 )

i try two times insert into this table row with same cell name value. Firs query already done, when second is queried i get only message from caught exception:

 could not execute statement

that is too few, that should help me to find what is wrong? I want get information from database something like

SQL Error. Duplicate column 'name' values

Hibernate with HibernateException just cover true cause error. Anyone know how to avoid situation like this?

Upvotes: 1

Views: 2601

Answers (1)

Michał Ziembiński
Michał Ziembiński

Reputation: 1154

You shuold getCause Hibernate Exception first, than you can get sql error status:

 catch (HibernateException ex) {

        Throwable cause = ex.getCause();
        if (cause instanceof SQLException) {
            System.out.println(cause.getMessage());
        }
 }

Upvotes: 3

Related Questions