Search in sources :

Example 91 with PersistenceException

use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.

the class AttributeConverterSqlTypeDescriptorAdapter method getExtractor.

// Extraction ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@Override
public <X> ValueExtractor<X> getExtractor(JavaTypeDescriptor<X> javaTypeDescriptor) {
    // Get the extractor for the intermediate type representation
    final ValueExtractor realExtractor = delegate.getExtractor(intermediateJavaTypeDescriptor);
    return new ValueExtractor<X>() {

        @Override
        public X extract(ResultSet rs, String name, WrapperOptions options) throws SQLException {
            return doConversion(realExtractor.extract(rs, name, options));
        }

        @Override
        public X extract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
            return doConversion(realExtractor.extract(statement, index, options));
        }

        @Override
        public X extract(CallableStatement statement, String[] paramNames, WrapperOptions options) throws SQLException {
            if (paramNames.length > 1) {
                throw new IllegalArgumentException("Basic value extraction cannot handle multiple output parameters");
            }
            return doConversion(realExtractor.extract(statement, paramNames, options));
        }

        @SuppressWarnings("unchecked")
        private X doConversion(Object extractedValue) {
            try {
                X convertedValue = (X) converter.convertToEntityAttribute(extractedValue);
                log.debugf("Converted value on extraction: %s -> %s", extractedValue, convertedValue);
                return convertedValue;
            } catch (PersistenceException pe) {
                throw pe;
            } catch (RuntimeException re) {
                throw new PersistenceException("Error attempting to apply AttributeConverter", re);
            }
        }
    };
}
Also used : CallableStatement(java.sql.CallableStatement) WrapperOptions(org.hibernate.type.descriptor.WrapperOptions) ResultSet(java.sql.ResultSet) PersistenceException(javax.persistence.PersistenceException) ValueExtractor(org.hibernate.type.descriptor.ValueExtractor)

Example 92 with PersistenceException

use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.

the class ExceptionConverterImpl method convert.

@Override
public RuntimeException convert(HibernateException e, LockOptions lockOptions) {
    Throwable cause = e;
    if (cause instanceof StaleStateException) {
        final PersistenceException converted = wrapStaleStateException((StaleStateException) cause);
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof LockingStrategyException) {
        final PersistenceException converted = wrapLockException((HibernateException) cause, lockOptions);
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof org.hibernate.exception.LockTimeoutException) {
        final PersistenceException converted = wrapLockException((HibernateException) cause, lockOptions);
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof org.hibernate.PessimisticLockException) {
        final PersistenceException converted = wrapLockException((HibernateException) cause, lockOptions);
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof org.hibernate.QueryTimeoutException) {
        final QueryTimeoutException converted = new QueryTimeoutException(cause.getMessage(), cause);
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof ObjectNotFoundException) {
        final EntityNotFoundException converted = new EntityNotFoundException(cause.getMessage());
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof org.hibernate.NonUniqueObjectException) {
        final EntityExistsException converted = new EntityExistsException(cause.getMessage());
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof org.hibernate.NonUniqueResultException) {
        final NonUniqueResultException converted = new NonUniqueResultException(cause.getMessage());
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof UnresolvableObjectException) {
        final EntityNotFoundException converted = new EntityNotFoundException(cause.getMessage());
        handlePersistenceException(converted);
        return converted;
    } else if (cause instanceof QueryException) {
        return new IllegalArgumentException(cause);
    } else if (cause instanceof MultipleBagFetchException) {
        return new IllegalArgumentException(cause);
    } else if (cause instanceof TransientObjectException) {
        try {
            sharedSessionContract.markForRollbackOnly();
        } catch (Exception ne) {
            //we do not want the subsequent exception to swallow the original one
            log.unableToMarkForRollbackOnTransientObjectException(ne);
        }
        //Spec 3.2.3 Synchronization rules
        return new IllegalStateException(e);
    } else {
        final PersistenceException converted = new PersistenceException(cause);
        handlePersistenceException(converted);
        return converted;
    }
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException) TransientObjectException(org.hibernate.TransientObjectException) HibernateException(org.hibernate.HibernateException) LockingStrategyException(org.hibernate.dialect.lock.LockingStrategyException) EntityNotFoundException(javax.persistence.EntityNotFoundException) EntityExistsException(javax.persistence.EntityExistsException) LockTimeoutException(javax.persistence.LockTimeoutException) MultipleBagFetchException(org.hibernate.loader.MultipleBagFetchException) NoResultException(javax.persistence.NoResultException) UnresolvableObjectException(org.hibernate.UnresolvableObjectException) SQLException(java.sql.SQLException) NonUniqueResultException(javax.persistence.NonUniqueResultException) JDBCException(org.hibernate.JDBCException) EntityNotFoundException(javax.persistence.EntityNotFoundException) StaleStateException(org.hibernate.StaleStateException) OptimisticEntityLockException(org.hibernate.dialect.lock.OptimisticEntityLockException) LockingStrategyException(org.hibernate.dialect.lock.LockingStrategyException) EntityExistsException(javax.persistence.EntityExistsException) OptimisticLockException(javax.persistence.OptimisticLockException) StaleObjectStateException(org.hibernate.StaleObjectStateException) PessimisticLockException(javax.persistence.PessimisticLockException) PessimisticEntityLockException(org.hibernate.dialect.lock.PessimisticEntityLockException) TransientObjectException(org.hibernate.TransientObjectException) PersistenceException(javax.persistence.PersistenceException) RollbackException(javax.persistence.RollbackException) QueryTimeoutException(javax.persistence.QueryTimeoutException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) HibernateException(org.hibernate.HibernateException) QueryException(org.hibernate.QueryException) QueryTimeoutException(javax.persistence.QueryTimeoutException) QueryException(org.hibernate.QueryException) StaleStateException(org.hibernate.StaleStateException) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) PersistenceException(javax.persistence.PersistenceException) UnresolvableObjectException(org.hibernate.UnresolvableObjectException) LockTimeoutException(javax.persistence.LockTimeoutException) MultipleBagFetchException(org.hibernate.loader.MultipleBagFetchException)

Example 93 with PersistenceException

use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.

the class ExceptionConverterImpl method wrapStaleStateException.

protected PersistenceException wrapStaleStateException(StaleStateException e) {
    PersistenceException pe;
    if (e instanceof StaleObjectStateException) {
        final StaleObjectStateException sose = (StaleObjectStateException) e;
        final Serializable identifier = sose.getIdentifier();
        if (identifier != null) {
            try {
                final Object entity = sharedSessionContract.internalLoad(sose.getEntityName(), identifier, false, true);
                if (entity instanceof Serializable) {
                    //avoid some user errors regarding boundary crossing
                    pe = new OptimisticLockException(e.getMessage(), e, entity);
                } else {
                    pe = new OptimisticLockException(e.getMessage(), e);
                }
            } catch (EntityNotFoundException enfe) {
                pe = new OptimisticLockException(e.getMessage(), e);
            }
        } else {
            pe = new OptimisticLockException(e.getMessage(), e);
        }
    } else {
        pe = new OptimisticLockException(e.getMessage(), e);
    }
    return pe;
}
Also used : Serializable(java.io.Serializable) PersistenceException(javax.persistence.PersistenceException) OptimisticLockException(javax.persistence.OptimisticLockException) EntityNotFoundException(javax.persistence.EntityNotFoundException) StaleObjectStateException(org.hibernate.StaleObjectStateException)

Example 94 with PersistenceException

use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.

the class ExceptionConverterImpl method wrapLockException.

protected PersistenceException wrapLockException(HibernateException e, LockOptions lockOptions) {
    final PersistenceException pe;
    if (e instanceof OptimisticEntityLockException) {
        final OptimisticEntityLockException lockException = (OptimisticEntityLockException) e;
        pe = new OptimisticLockException(lockException.getMessage(), lockException, lockException.getEntity());
    } else if (e instanceof org.hibernate.exception.LockTimeoutException) {
        pe = new LockTimeoutException(e.getMessage(), e, null);
    } else if (e instanceof PessimisticEntityLockException) {
        final PessimisticEntityLockException lockException = (PessimisticEntityLockException) e;
        if (lockOptions != null && lockOptions.getTimeOut() > -1) {
            // assume lock timeout occurred if a timeout or NO WAIT was specified
            pe = new LockTimeoutException(lockException.getMessage(), lockException, lockException.getEntity());
        } else {
            pe = new PessimisticLockException(lockException.getMessage(), lockException, lockException.getEntity());
        }
    } else if (e instanceof org.hibernate.PessimisticLockException) {
        final org.hibernate.PessimisticLockException jdbcLockException = (org.hibernate.PessimisticLockException) e;
        if (lockOptions != null && lockOptions.getTimeOut() > -1) {
            // assume lock timeout occurred if a timeout or NO WAIT was specified
            pe = new LockTimeoutException(jdbcLockException.getMessage(), jdbcLockException, null);
        } else {
            pe = new PessimisticLockException(jdbcLockException.getMessage(), jdbcLockException, null);
        }
    } else {
        pe = new OptimisticLockException(e);
    }
    return pe;
}
Also used : PessimisticEntityLockException(org.hibernate.dialect.lock.PessimisticEntityLockException) PersistenceException(javax.persistence.PersistenceException) OptimisticLockException(javax.persistence.OptimisticLockException) LockTimeoutException(javax.persistence.LockTimeoutException) OptimisticEntityLockException(org.hibernate.dialect.lock.OptimisticEntityLockException) PessimisticLockException(javax.persistence.PessimisticLockException)

Example 95 with PersistenceException

use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.

the class DataSourceInjectionTest method testDatasourceInjection.

@Test
public void testDatasourceInjection() throws Exception {
    File current = new File(".");
    File sub = new File(current, "puroot");
    sub.mkdir();
    PersistenceUnitInfoImpl info = new PersistenceUnitInfoImpl(sub.toURI().toURL(), new String[] {});
    try {
        emf = new HibernatePersistenceProvider().createContainerEntityManagerFactory(info, null);
        try {
            emf.createEntityManager().createQuery("select i from Item i").getResultList();
        } finally {
            try {
                emf.close();
            } catch (Exception ignore) {
                int i = 0;
            }
        }
        Assert.fail("FakeDatasource should have been used");
    } catch (PersistenceException pe) {
        if (emf != null) {
            emf.close();
        }
        Assert.assertTrue(pe.getCause() instanceof FakeDataSourceException);
    } catch (FakeDataSourceException fde) {
    //success
    } finally {
        sub.delete();
    }
}
Also used : PersistenceException(javax.persistence.PersistenceException) HibernatePersistenceProvider(org.hibernate.jpa.HibernatePersistenceProvider) File(java.io.File) PersistenceException(javax.persistence.PersistenceException) Test(org.junit.Test)

Aggregations

PersistenceException (javax.persistence.PersistenceException)125 Test (org.junit.Test)66 Session (org.hibernate.Session)50 Transaction (org.hibernate.Transaction)29 EntityManager (javax.persistence.EntityManager)17 IOException (java.io.IOException)12 StaleObjectStateException (org.hibernate.StaleObjectStateException)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)9 SQLGrammarException (org.hibernate.exception.SQLGrammarException)8 TransactionRequiredException (javax.persistence.TransactionRequiredException)7 MessagesEvent (com.openmeap.event.MessagesEvent)5 EntityNotFoundException (javax.persistence.EntityNotFoundException)5 OptimisticLockException (javax.persistence.OptimisticLockException)5 TestForIssue (org.hibernate.testing.TestForIssue)5 GlobalSettings (com.openmeap.model.dto.GlobalSettings)4 NoResultException (javax.persistence.NoResultException)4 LockOptions (org.hibernate.LockOptions)4 StaleStateException (org.hibernate.StaleStateException)4