001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.orm.hibernate3; 018 019import org.hibernate.HibernateException; 020import org.hibernate.JDBCException; 021 022import org.springframework.dao.DataAccessException; 023import org.springframework.dao.support.PersistenceExceptionTranslator; 024import org.springframework.jdbc.support.SQLExceptionTranslator; 025 026/** 027 * {@link PersistenceExceptionTranslator} capable of translating {@link HibernateException} 028 * instances to Spring's {@link DataAccessException} hierarchy. 029 * 030 * <p>Extended by {@link LocalSessionFactoryBean}, so there is no need to declare this 031 * translator in addition to a {@code LocalSessionFactoryBean}. 032 * 033 * <p>When configuring the container with {@code @Configuration} classes, a {@code @Bean} 034 * of this type must be registered manually. 035 * 036 * @author Juergen Hoeller 037 * @author Chris Beams 038 * @since 3.1 039 * @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor 040 * @see SessionFactoryUtils#convertHibernateAccessException(HibernateException) 041 * @see SQLExceptionTranslator 042 * @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x 043 */ 044@Deprecated 045public class HibernateExceptionTranslator implements PersistenceExceptionTranslator { 046 047 private SQLExceptionTranslator jdbcExceptionTranslator; 048 049 050 /** 051 * Set the JDBC exception translator for the SessionFactory, 052 * exposed via the PersistenceExceptionTranslator interface. 053 * <p>Applied to any SQLException root cause of a Hibernate JDBCException, 054 * overriding Hibernate's default SQLException translation (which is 055 * based on Hibernate's Dialect for a specific target database). 056 * @param jdbcExceptionTranslator the exception translator 057 * @see java.sql.SQLException 058 * @see org.hibernate.JDBCException 059 * @see org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator 060 * @see org.springframework.jdbc.support.SQLStateSQLExceptionTranslator 061 * @see org.springframework.dao.support.PersistenceExceptionTranslator 062 */ 063 public void setJdbcExceptionTranslator(SQLExceptionTranslator jdbcExceptionTranslator) { 064 this.jdbcExceptionTranslator = jdbcExceptionTranslator; 065 } 066 067 068 @Override 069 public DataAccessException translateExceptionIfPossible(RuntimeException ex) { 070 if (ex instanceof HibernateException) { 071 return convertHibernateAccessException((HibernateException) ex); 072 } 073 return null; 074 } 075 076 /** 077 * Convert the given HibernateException to an appropriate exception from the 078 * {@code org.springframework.dao} hierarchy. 079 * <p>Will automatically apply a specified SQLExceptionTranslator to a 080 * Hibernate JDBCException, else rely on Hibernate's default translation. 081 * @param ex HibernateException that occured 082 * @return a corresponding DataAccessException 083 * @see SessionFactoryUtils#convertHibernateAccessException 084 * @see #setJdbcExceptionTranslator 085 */ 086 protected DataAccessException convertHibernateAccessException(HibernateException ex) { 087 if (this.jdbcExceptionTranslator != null && ex instanceof JDBCException) { 088 JDBCException jdbcEx = (JDBCException) ex; 089 return this.jdbcExceptionTranslator.translate( 090 "Hibernate operation: " + jdbcEx.getMessage(), jdbcEx.getSQL(), jdbcEx.getSQLException()); 091 } 092 return SessionFactoryUtils.convertHibernateAccessException(ex); 093 } 094 095}