use of org.hibernate.ObjectNotFoundException 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 org.hibernate.ObjectNotFoundException in project hibernate-orm by hibernate.
the class SessionImpl method find.
@Override
public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockModeType, Map<String, Object> properties) {
checkOpen();
LockOptions lockOptions = null;
try {
if (properties != null && !properties.isEmpty()) {
getLoadQueryInfluencers().setFetchGraph((EntityGraph) properties.get(QueryHints.HINT_FETCHGRAPH));
getLoadQueryInfluencers().setLoadGraph((EntityGraph) properties.get(QueryHints.HINT_LOADGRAPH));
}
final IdentifierLoadAccess<T> loadAccess = byId(entityClass);
loadAccess.with(determineAppropriateLocalCacheMode(properties));
if (lockModeType != null) {
if (!LockModeType.NONE.equals(lockModeType)) {
checkTransactionNeeded();
}
lockOptions = buildLockOptions(lockModeType, properties);
loadAccess.with(lockOptions);
}
return loadAccess.load((Serializable) primaryKey);
} catch (EntityNotFoundException ignored) {
// which find() should not throw. Find() should return null if the entity was not found.
if (log.isDebugEnabled()) {
String entityName = entityClass != null ? entityClass.getName() : null;
String identifierValue = primaryKey != null ? primaryKey.toString() : null;
log.ignoringEntityNotFound(entityName, identifierValue);
}
return null;
} catch (ObjectDeletedException e) {
//the spec is silent about people doing remove() find() on the same PC
return null;
} catch (ObjectNotFoundException e) {
//should not happen on the entity itself with get
throw new IllegalArgumentException(e.getMessage(), e);
} catch (MappingException | TypeMismatchException | ClassCastException e) {
throw exceptionConverter.convert(new IllegalArgumentException(e.getMessage(), e));
} catch (RuntimeException e) {
throw exceptionConverter.convert(e, lockOptions);
} finally {
getLoadQueryInfluencers().setFetchGraph(null);
getLoadQueryInfluencers().setLoadGraph(null);
}
}
use of org.hibernate.ObjectNotFoundException in project hibernate-orm by hibernate.
the class LoaderTest method testGetNotExisting.
@Test
public void testGetNotExisting() {
Session s = openSession();
s.beginTransaction();
try {
long notExistingId = 1l;
s.load(Team.class, notExistingId);
s.get(Team.class, notExistingId);
s.getTransaction().commit();
} catch (ObjectNotFoundException e) {
if (s.getTransaction().getStatus() == TransactionStatus.ACTIVE) {
s.getTransaction().rollback();
}
fail("#get threw an ObjectNotFoundExcepton");
} finally {
s.close();
}
}
Aggregations