Francesco
Francesco

Reputation: 1792

JPA/Hibernate exception handling

I'm using JPA/Hibernate (a newbie at them). When an exception occur (could be an unique constraint violation) I want to catch it and show some application meaning messages, instead of printing the stack trace.

Does Hibernate provide some tool to get info (maybe database independent) about exceptions?

Upvotes: 9

Views: 50138

Answers (6)

Mahdi
Mahdi

Reputation: 65

You can catch Hibernate Exceptions when you want to call Flush() on your Session or commit() on your Transaction.

try {
    session.getTransaction().commit();
} catch (Exception e) {
    System.out.println("Hibernate Exception: " + e.getMessage());
}
    
// or

try {
    session.flush();
} catch (Exception e) {
    System.out.println("Hibernate Exception: " + e.getMessage());
}

Upvotes: 0

user3145382
user3145382

Reputation: 41

You can do like following. i did that. But offcourse you are using vendor specific code so if you go with a different JPS provider, you will have to change code at several places. At the same time, sometimes its practical when you know you are not gonna change JPA provider easily and user friendly error message is more important

try{
...
}

catch( javax.persistence.PersistenceException  ex)

{

     if(ex.getCause() instanceof org.hibernate.exception.ConstraintViolationException)

      {..........}

}

Upvotes: 4

lanoxx
lanoxx

Reputation: 13051

You can either catch the general JDBCException:

try {
    userDao.save(user); //might throw exception
} catch(JDBCException e) {
    //Error during hibernate query
}

or you can also catch one of the more specific subclasses of JDBCException such as ConstraintViolationException or JDBCConnectionException:

try {
    userDao.save(user); //might throw exception
} catch(ConstraintViolationException e) {
    //Email Address already exists
} catch(JDBCConnectionException e) {
    //Lost the connection
}

and with the e.getCause() method you can retrieve the underlying SQLException and analyse it further:

try {
    userDao.save(user); //might throw exception
} catch(JDBCException e) {
    SQLException cause = (SQLException) e.getCause();
    //evaluate cause and find out what was the problem
    System.out.println(cause.getMessage());
}

Which would print for example: Duplicate entry 'UserTestUsername' for key 'username'

Upvotes: 3

Nathan Hughes
Nathan Hughes

Reputation: 96385

You can specifically catch org.hibernate.exception.ConstraintViolationException. That way you know you're catching only constraint issues.

Upvotes: 1

A Null Pointer
A Null Pointer

Reputation: 2277

All exceptions in Hibernate are derivatives of the Java's RuntimeException class. So if you catch a RuntimeException in your code , you can fetch the cause of the exception by calling the Exception class' getCause() or getMessage() methods

Upvotes: -1

Aravind Yarram
Aravind Yarram

Reputation: 80176

HibernateException encapsulates the actual root cause than can provide you enough information for you to generate meaningful user-friendly messages. Read the Exception Handling section of their documentation.

Upvotes: 6

Related Questions