|
| 1 | +/* |
| 2 | + * Copyright 2002-2011 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.orm.hibernate3; |
| 18 | + |
| 19 | +import org.hibernate.HibernateException; |
| 20 | +import org.hibernate.JDBCException; |
| 21 | +import org.springframework.dao.DataAccessException; |
| 22 | +import org.springframework.dao.support.PersistenceExceptionTranslator; |
| 23 | +import org.springframework.jdbc.support.SQLExceptionTranslator; |
| 24 | + |
| 25 | +/** |
| 26 | + * {@link PersistenceExceptionTranslator} capable of translating {@link HibernateException} |
| 27 | + * instances to Spring's {@link DataAccessException} hierarchy. |
| 28 | + * |
| 29 | + * <p>When configuring the Spring container via XML, note that this translator is |
| 30 | + * automatically used internally by {@link SessionFactoryBean} types. When configuring |
| 31 | + * the container with {@code @Configuration} classes, a {@code @Bean} of this type |
| 32 | + * must be registered manually. |
| 33 | + * |
| 34 | + * @author Chris Beams |
| 35 | + * @since 3.1 |
| 36 | + * @see SessionFactoryBuilder |
| 37 | + * @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor |
| 38 | + * @see SessionFactoryUtils#convertHibernateAccessException(HibernateException) |
| 39 | + * @see SQLExceptionTranslator |
| 40 | + */ |
| 41 | +public class HibernateExceptionTranslator implements PersistenceExceptionTranslator { |
| 42 | + |
| 43 | + private SQLExceptionTranslator jdbcExceptionTranslator; |
| 44 | + |
| 45 | + public DataAccessException translateExceptionIfPossible(RuntimeException ex) { |
| 46 | + if (ex instanceof HibernateException) { |
| 47 | + return convertHibernateAccessException((HibernateException) ex); |
| 48 | + } |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Convert the given HibernateException to an appropriate exception from the |
| 54 | + * {@code org.springframework.dao} hierarchy. |
| 55 | + * <p>Will automatically apply a specified SQLExceptionTranslator to a |
| 56 | + * Hibernate JDBCException, else rely on Hibernate's default translation. |
| 57 | + * @param ex HibernateException that occured |
| 58 | + * @return a corresponding DataAccessException |
| 59 | + * @see SessionFactoryUtils#convertHibernateAccessException |
| 60 | + * @see #setJdbcExceptionTranslator |
| 61 | + */ |
| 62 | + protected DataAccessException convertHibernateAccessException(HibernateException ex) { |
| 63 | + if (this.jdbcExceptionTranslator != null && ex instanceof JDBCException) { |
| 64 | + JDBCException jdbcEx = (JDBCException) ex; |
| 65 | + return this.jdbcExceptionTranslator.translate( |
| 66 | + "Hibernate operation: " + jdbcEx.getMessage(), jdbcEx.getSQL(), jdbcEx.getSQLException()); |
| 67 | + } |
| 68 | + return SessionFactoryUtils.convertHibernateAccessException(ex); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Set the JDBC exception translator for the SessionFactory, |
| 73 | + * exposed via the PersistenceExceptionTranslator interface. |
| 74 | + * <p>Applied to any SQLException root cause of a Hibernate JDBCException, |
| 75 | + * overriding Hibernate's default SQLException translation (which is |
| 76 | + * based on Hibernate's Dialect for a specific target database). |
| 77 | + * @param jdbcExceptionTranslator the exception translator |
| 78 | + * @see java.sql.SQLException |
| 79 | + * @see org.hibernate.JDBCException |
| 80 | + * @see org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator |
| 81 | + * @see org.springframework.jdbc.support.SQLStateSQLExceptionTranslator |
| 82 | + * @see org.springframework.dao.support.PersistenceExceptionTranslator |
| 83 | + */ |
| 84 | + public void setJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator) { |
| 85 | + this.jdbcExceptionTranslator = jdbcExceptionTranslator; |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments