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?
When Hibernate interacts with the database it throws SQLException. Hibernate provides better handle than the JDBCException. Developers can use the try and catch block to handle the exceptions. Put the line of code that may cause an exception in a try block and according to that exception put the exception handler in the catch block.
These exceptions mostly extend from HibernateException. However, if we're using Hibernate as a JPA persistence provider, these exceptions may get wrapped into PersistenceException. Both of these base classes extend from RuntimeException . Therefore, they're all unchecked. Hence, we don't need to catch or declare them at every place they're used.
Think of JPA as the guidelines that must be followed or an interface, while Hibernate's JPA implementation is code that meets the API as defined by the JPA specification and provides the under the hood functionality. In short, JPA is the interface while Hibernate is the implementation.
These can be mapping errors, infrastructure problems, SQL errors, data integrity violations, session problems, and transaction errors. These exceptions mostly extend from HibernateException. However, if we're using Hibernate as a JPA persistence provider, these exceptions may get wrapped into PersistenceException.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With