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);
}
}
};
}
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;
}
}
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;
}
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;
}
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();
}
}
Aggregations