Hibernate will wrap any SQLExceptions into a org.hibernate.JDBCException
. So you want to catch this Exception type. If you explicitly need to access an actual PSQLException, you can retrieve it through the getSQLException()
Method:
try {
String nativeSql = "Some random non-sql stuff";
Session sess = sessionFactory.openSession();
List <?> result = sess.createSQLQuery(nativeSql).list();
} catch (JDBCException e) {
SQLException sqlException = e.getSQLException;
if (sqlException instanceof PSQLException) {
PSQLException psqlException = (PSQLException) sqlException;
// ... work with psqlException ...
}
}