HibernateExceptionTranslator converts JPA exceptions as well (for Hibernate 5.2)
Issue: SPR-14455
This commit is contained in:
		
							parent
							
								
									4d6d5e0ddd
								
							
						
					
					
						commit
						1d39d762f0
					
				| 
						 | 
					@ -1,5 +1,5 @@
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
 * Copyright 2002-2015 the original author or authors.
 | 
					 * Copyright 2002-2016 the original author or authors.
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
					 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
				
			||||||
 * you may not use this file except in compliance with the License.
 | 
					 * you may not use this file except in compliance with the License.
 | 
				
			||||||
| 
						 | 
					@ -16,14 +16,18 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
package org.springframework.orm.hibernate5;
 | 
					package org.springframework.orm.hibernate5;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import javax.persistence.PersistenceException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import org.hibernate.HibernateException;
 | 
					import org.hibernate.HibernateException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import org.springframework.dao.DataAccessException;
 | 
					import org.springframework.dao.DataAccessException;
 | 
				
			||||||
import org.springframework.dao.support.PersistenceExceptionTranslator;
 | 
					import org.springframework.dao.support.PersistenceExceptionTranslator;
 | 
				
			||||||
 | 
					import org.springframework.orm.jpa.EntityManagerFactoryUtils;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * {@link PersistenceExceptionTranslator} capable of translating {@link HibernateException}
 | 
					 * {@link PersistenceExceptionTranslator} capable of translating {@link HibernateException}
 | 
				
			||||||
 * instances to Spring's {@link DataAccessException} hierarchy.
 | 
					 * instances to Spring's {@link DataAccessException} hierarchy. As of Spring 4.3.2 and
 | 
				
			||||||
 | 
					 * Hibernate 5.2, it also converts standard JPA {@link PersistenceException} instances.
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * <p>Extended by {@link LocalSessionFactoryBean}, so there is no need to declare this
 | 
					 * <p>Extended by {@link LocalSessionFactoryBean}, so there is no need to declare this
 | 
				
			||||||
 * translator in addition to a {@code LocalSessionFactoryBean}.
 | 
					 * translator in addition to a {@code LocalSessionFactoryBean}.
 | 
				
			||||||
| 
						 | 
					@ -35,6 +39,7 @@ import org.springframework.dao.support.PersistenceExceptionTranslator;
 | 
				
			||||||
 * @since 4.2
 | 
					 * @since 4.2
 | 
				
			||||||
 * @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
 | 
					 * @see org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
 | 
				
			||||||
 * @see SessionFactoryUtils#convertHibernateAccessException(HibernateException)
 | 
					 * @see SessionFactoryUtils#convertHibernateAccessException(HibernateException)
 | 
				
			||||||
 | 
					 * @see EntityManagerFactoryUtils#convertJpaAccessExceptionIfPossible(RuntimeException)
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public class HibernateExceptionTranslator implements PersistenceExceptionTranslator {
 | 
					public class HibernateExceptionTranslator implements PersistenceExceptionTranslator {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -43,13 +48,16 @@ public class HibernateExceptionTranslator implements PersistenceExceptionTransla
 | 
				
			||||||
		if (ex instanceof HibernateException) {
 | 
							if (ex instanceof HibernateException) {
 | 
				
			||||||
			return convertHibernateAccessException((HibernateException) ex);
 | 
								return convertHibernateAccessException((HibernateException) ex);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return null;
 | 
							if (ex instanceof PersistenceException && ex.getCause() instanceof HibernateException) {
 | 
				
			||||||
 | 
								return convertHibernateAccessException((HibernateException) ex.getCause());
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(ex);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Convert the given HibernateException to an appropriate exception from the
 | 
						 * Convert the given HibernateException to an appropriate exception from the
 | 
				
			||||||
	 * {@code org.springframework.dao} hierarchy.
 | 
						 * {@code org.springframework.dao} hierarchy.
 | 
				
			||||||
	 * @param ex HibernateException that occured
 | 
						 * @param ex HibernateException that occurred
 | 
				
			||||||
	 * @return a corresponding DataAccessException
 | 
						 * @return a corresponding DataAccessException
 | 
				
			||||||
	 * @see SessionFactoryUtils#convertHibernateAccessException
 | 
						 * @see SessionFactoryUtils#convertHibernateAccessException
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue