Search in sources :

Example 36 with EntityNotFoundException

use of javax.persistence.EntityNotFoundException in project API by ca-cwds.

the class AllegationPerpetratorHistoryService method update.

/**
   * {@inheritDoc}
   * 
   * @see gov.ca.cwds.rest.services.CrudsService#update(java.io.Serializable,
   *      gov.ca.cwds.rest.api.Request)
   */
@Override
public gov.ca.cwds.rest.api.domain.cms.AllegationPerpetratorHistory update(Serializable primaryKey, Request request) {
    assert primaryKey instanceof String;
    assert request instanceof gov.ca.cwds.rest.api.domain.cms.AllegationPerpetratorHistory;
    gov.ca.cwds.rest.api.domain.cms.AllegationPerpetratorHistory allegationPerpetratorHistory = (gov.ca.cwds.rest.api.domain.cms.AllegationPerpetratorHistory) request;
    try {
        String lastUpdatedId = staffPersonIdRetriever.getStaffPersonId();
        AllegationPerpetratorHistory managed = new AllegationPerpetratorHistory((String) primaryKey, allegationPerpetratorHistory, lastUpdatedId);
        managed = allegationPerpetratorHistoryDao.update(managed);
        return new gov.ca.cwds.rest.api.domain.cms.AllegationPerpetratorHistory(managed);
    } catch (EntityNotFoundException e) {
        LOGGER.info("AllegationPerpetratorHistory not found : {}", allegationPerpetratorHistory);
        throw new ServiceException(e);
    }
}
Also used : ServiceException(gov.ca.cwds.rest.services.ServiceException) AllegationPerpetratorHistory(gov.ca.cwds.data.persistence.cms.AllegationPerpetratorHistory) PostedAllegationPerpetratorHistory(gov.ca.cwds.rest.api.domain.cms.PostedAllegationPerpetratorHistory) EntityNotFoundException(javax.persistence.EntityNotFoundException)

Example 37 with EntityNotFoundException

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

the class EntityManagerTest method testEntityNotFoundException.

@Test
public void testEntityNotFoundException() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Wallet w = new Wallet();
    w.setBrand("Lacoste");
    w.setModel("Minimic");
    w.setSerial("0324");
    em.persist(w);
    Wallet wallet = em.find(Wallet.class, w.getSerial());
    em.createNativeQuery("delete from Wallet").executeUpdate();
    try {
        em.refresh(wallet);
    } catch (EntityNotFoundException enfe) {
        // success
        if (em.getTransaction() != null) {
            em.getTransaction().rollback();
        }
        em.close();
        return;
    }
    try {
        em.getTransaction().commit();
        fail("Should have raised an EntityNotFoundException");
    } catch (PersistenceException pe) {
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) PersistenceException(javax.persistence.PersistenceException) EntityNotFoundException(javax.persistence.EntityNotFoundException) Test(org.junit.Test)

Example 38 with EntityNotFoundException

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

the class EntityManagerTest method testGet.

@Test
@TestForIssue(jiraKey = "EJB-9")
public void testGet() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    Item item = em.getReference(Item.class, "nonexistentone");
    try {
        item.getDescr();
        em.getTransaction().commit();
        fail("Object with wrong id should have failed");
    } catch (EntityNotFoundException e) {
        //success
        if (em.getTransaction() != null) {
            em.getTransaction().rollback();
        }
    } finally {
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) EntityNotFoundException(javax.persistence.EntityNotFoundException) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 39 with EntityNotFoundException

use of javax.persistence.EntityNotFoundException 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 40 with EntityNotFoundException

use of javax.persistence.EntityNotFoundException 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)

Aggregations

EntityNotFoundException (javax.persistence.EntityNotFoundException)73 EntityManager (javax.persistence.EntityManager)20 NonexistentEntityException (com.intel.mtwilson.as.controller.exceptions.NonexistentEntityException)18 ServiceException (gov.ca.cwds.rest.services.ServiceException)15 Transactional (org.springframework.transaction.annotation.Transactional)14 BusinessServiceException (sic.service.BusinessServiceException)11 Test (org.junit.Test)10 Calendar (java.util.Calendar)9 GregorianCalendar (java.util.GregorianCalendar)9 BooleanBuilder (com.querydsl.core.BooleanBuilder)6 TblMle (com.intel.mtwilson.as.data.TblMle)5 ArrayList (java.util.ArrayList)5 IllegalOrphanException (com.intel.mtwilson.as.controller.exceptions.IllegalOrphanException)4 TblModuleManifest (com.intel.mtwilson.as.data.TblModuleManifest)4 PersistenceException (javax.persistence.PersistenceException)4 Sort (org.springframework.data.domain.Sort)4 TblHosts (com.intel.mtwilson.as.data.TblHosts)3 OptimisticLockException (javax.persistence.OptimisticLockException)3 TblEventType (com.intel.mtwilson.as.data.TblEventType)2 TblHostSpecificManifest (com.intel.mtwilson.as.data.TblHostSpecificManifest)2