001 package org.nanocontainer.persistence.hibernate.classic;
002
003 import net.sf.hibernate.StaleObjectStateException;
004 import net.sf.hibernate.TransactionException;
005 import net.sf.hibernate.UnresolvableObjectException;
006 import net.sf.hibernate.WrongClassException;
007
008 import net.sf.hibernate.exception.LockAcquisitionException;
009 import org.nanocontainer.persistence.ExceptionFactory;
010
011
012 /**
013 * ExceptionHandler version for Hibernate 2.
014 *
015 * @version $Revision: $
016 * @see org.nanocontainer.persistence.ExceptionHandler
017 */
018 public class HibernateExceptionHandler {
019
020 private ExceptionFactory exceptionFactory;
021
022 public HibernateExceptionHandler(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.getPersistentClass().getName(), 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.getPersistentClass().getName(), e.getIdentifier());
043 }
044
045 if (ex instanceof WrongClassException) {
046 WrongClassException e = (WrongClassException) ex;
047 return exceptionFactory.createObjectRetrievalFailureException(e, e.getPersistentClass().getName(), 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 }