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