0

Consider an hypothetical User table:

-- postgres, but it could have been any other SQL database
CREATE TABLE User(
  id SERIAL PRIMARY KEY,
  mail VARCHAR(32) UNIQUE NOT NULL
);

Let's assume I attempt to add two users with the same mail:

session.save(new User(1, "[email protected]"));
session.save(new User(2, "[email protected]"));

and execute it through Hibernate. Hibernate will throw me an ConstraintViolationException:

Exception in thread "main" org.hibernate.exception.ConstraintViolationException: could not execute statement
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:129)

...

Caused by: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "users_mail_key"
  Detail: Key (mail)=([email protected]) already exists.
        at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2198)
    ...

What I'd like to know is if there's some good way, other than having to manually parse the Exception's output text, to gather what is the reason of the error so I may correctly interpret and react to the problem.

I realize that this may actually be more of a Postgres Driver's problem than actually an Hibernate one, but I'm unsure at this stage so I thought it may opportune to ask in Hibernate's context.

3
  • Can you inspect the exception type (ConstraintViolationException)? Does that provide enough info for you? Commented Feb 4, 2015 at 0:50
  • I did. From what I've gathered, even in the inner exception, nothing too useful is found. That's why I said that maybe the problem can be that Postgres Driver may not be very information-rich. Maybe with other drivers for other DBs this doesn't happen. Commented Feb 4, 2015 at 1:00
  • What does it return if you call getErrorCode and getSQLState for this Exception from Postgresql Driver? Commented Feb 4, 2015 at 1:55

2 Answers 2

1

So if you are able to get a value from getSQLState, you can handle the exception:

"All messages emitted by the PostgreSQL server are assigned five-character error codes that follow the SQL standard's conventions for "SQLSTATE" codes. Applications that need to know which error condition has occurred should usually test the error code, rather than looking at the textual error message."

From: http://www.postgresql.org/docs/9.3/static/errcodes-appendix.html

23505 = unique_violation

Note: In this link there is also the list.

Sign up to request clarification or add additional context in comments.

Comments

0

Well, after looking at Postgres Driver's source code it seems the problem lies with Postgres and not with Hibernate. PSQLException will contain some information, although it certainly isn't as polished as I first assumed :(

    } catch (PSQLException e) {
        ServerErrorMessage m = e.getServerErrorMessage();
        System.out.println(m.getColumn());
        System.out.println(m.getConstraint());
        System.out.println(m.getDatatype());
        System.out.println(m.getDetail());
        System.out.println(m.getFile());
        System.out.println(m.getHint());
        System.out.println(m.getInternalPosition());
        System.out.println(m.getInternalQuery());
        System.out.println(m.getLine());
        System.out.println(m.getMessage());
        System.out.println(m.getPosition());
        System.out.println(m.getRoutine());
        System.out.println(m.getSchema());
        System.out.println(m.getSeverity());
        System.out.println(m.getSQLState());
        System.out.println(m.getTable());
        System.out.println(m.getWhere());
    }

prints

null
users_mail_key
null
Key (mail)=([email protected]) already exists.
nbtinsert.c
null
0
null
398
duplicate key value violates unique constraint "users_mail_key"
0
_bt_check_unique
public
ERROR
23505
users
null

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.