Search in sources :

Example 11 with ObjectNotFoundException

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;
    }
}
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 12 with ObjectNotFoundException

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);
    }
}
Also used : LockOptions(org.hibernate.LockOptions) TypeMismatchException(org.hibernate.TypeMismatchException) EntityNotFoundException(javax.persistence.EntityNotFoundException) UnknownSqlResultSetMappingException(org.hibernate.procedure.UnknownSqlResultSetMappingException) MappingException(org.hibernate.MappingException) JPA_LOCK_TIMEOUT(org.hibernate.cfg.AvailableSettings.JPA_LOCK_TIMEOUT) ObjectNotFoundException(org.hibernate.ObjectNotFoundException) ObjectDeletedException(org.hibernate.ObjectDeletedException)

Example 13 with ObjectNotFoundException

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();
    }
}
Also used : ObjectNotFoundException(org.hibernate.ObjectNotFoundException) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

ObjectNotFoundException (org.hibernate.ObjectNotFoundException)13 Session (org.hibernate.Session)9 Test (org.junit.Test)9 SQLException (java.sql.SQLException)2 Date (java.util.Date)2 EntityNotFoundException (javax.persistence.EntityNotFoundException)2 OnmsAlarm (org.opennms.netmgt.model.OnmsAlarm)2 Serializable (java.io.Serializable)1 InetAddress (java.net.InetAddress)1 Connection (java.sql.Connection)1 ArrayList (java.util.ArrayList)1 EntityExistsException (javax.persistence.EntityExistsException)1 LockTimeoutException (javax.persistence.LockTimeoutException)1 NoResultException (javax.persistence.NoResultException)1 NonUniqueResultException (javax.persistence.NonUniqueResultException)1 OptimisticLockException (javax.persistence.OptimisticLockException)1 PersistenceException (javax.persistence.PersistenceException)1 PessimisticLockException (javax.persistence.PessimisticLockException)1 QueryTimeoutException (javax.persistence.QueryTimeoutException)1 RollbackException (javax.persistence.RollbackException)1