001 package org.nanocontainer.persistence.hibernate;
002
003 import org.hibernate.StaleObjectStateException;
004 import org.hibernate.TransactionException;
005 import org.hibernate.UnresolvableObjectException;
006 import org.hibernate.WrongClassException;
007 import org.hibernate.exception.LockAcquisitionException;
008 import org.nanocontainer.persistence.ExceptionFactory;
009 import org.nanocontainer.persistence.ExceptionHandler;
010
011 /**
012 * Default Hibernate 3 ExceptionHandler.
013 *
014 * @see org.nanocontainer.persistence.ExceptionHandler
015 *
016 * @version $Id: DefaultHibernateExceptionHandler.java 2510 2005-09-22 10:11:19Z mauro $
017 */
018 public class DefaultHibernateExceptionHandler implements ExceptionHandler {
019
020 private ExceptionFactory exceptionFactory;
021
022 public DefaultHibernateExceptionHandler(ExceptionFactory exceptionFactory) {
023 this.exceptionFactory = exceptionFactory;
024 }
025
026 public RuntimeException handle(Throwable ex) {
027
028 // Optimistic locking cases.
029 if (ex instanceof StaleObjectStateException) {
030 StaleObjectStateException e = (StaleObjectStateException) ex;
031 return exceptionFactory.createStaleObjectStateException(e, e.getEntityName(), e.getIdentifier());
032 }
033
034 if (ex instanceof LockAcquisitionException) {
035 LockAcquisitionException e = (LockAcquisitionException) ex;
036 return exceptionFactory.createConcurrencyFailureException(e);
037 }
038
039 // Object retrieval failure cases.
040 if (ex instanceof UnresolvableObjectException) {
041 UnresolvableObjectException e = (UnresolvableObjectException) ex;
042 return exceptionFactory.createObjectRetrievalFailureException(e, e.getEntityName(), e.getIdentifier());
043 }
044
045 if (ex instanceof WrongClassException) {
046 WrongClassException e = (WrongClassException) ex;
047 return exceptionFactory.createObjectRetrievalFailureException(e, e.getEntityName(), e.getIdentifier());
048 }
049
050 // Transaction
051 if (ex instanceof TransactionException) {
052 return exceptionFactory.createTransactionException(ex);
053 }
054
055 // Otherwise, return a generic persistence exception
056 return exceptionFactory.createPersistenceException(ex);
057 }
058
059 }