use of javax.persistence.EntityExistsException in project API by ca-cwds.
the class AllegationService method create.
/**
* {@inheritDoc}
*
* @see gov.ca.cwds.rest.services.CrudsService#create(gov.ca.cwds.rest.api.Request)
*/
@Override
public PostedAllegation create(Request request) {
assert request instanceof gov.ca.cwds.rest.api.domain.cms.Allegation;
gov.ca.cwds.rest.api.domain.cms.Allegation allegation = (gov.ca.cwds.rest.api.domain.cms.Allegation) request;
try {
String lastUpdatedId = staffPersonIdRetriever.getStaffPersonId();
Allegation managed = new Allegation(CmsKeyIdGenerator.cmsIdGenertor(lastUpdatedId), allegation, lastUpdatedId);
managed = allegationDao.create(managed);
return new PostedAllegation(managed);
} catch (EntityExistsException e) {
LOGGER.info("Allegation already exists : {}", allegation);
throw new ServiceException(e);
}
}
use of javax.persistence.EntityExistsException 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.EntityExistsException in project CloudStack-archive by CloudStack-extras.
the class GenericDaoBase method update.
public int update(UpdateBuilder ub, final SearchCriteria<?> sc, Integer rows) {
StringBuilder sql = null;
PreparedStatement pstmt = null;
final Transaction txn = Transaction.currentTxn();
try {
final String searchClause = sc.getWhereClause();
sql = ub.toSql(_tables);
if (sql == null) {
return 0;
}
sql.append(searchClause);
if (rows != null) {
sql.append(" LIMIT ").append(rows);
}
txn.start();
pstmt = txn.prepareAutoCloseStatement(sql.toString());
Collection<Ternary<Attribute, Boolean, Object>> changes = ub.getChanges();
int i = 1;
for (final Ternary<Attribute, Boolean, Object> value : changes) {
prepareAttribute(i++, pstmt, value.first(), value.third());
}
for (Pair<Attribute, Object> value : sc.getValues()) {
prepareAttribute(i++, pstmt, value.first(), value.second());
}
int result = pstmt.executeUpdate();
txn.commit();
ub.clear();
return result;
} catch (final SQLException e) {
if (e.getSQLState().equals("23000") && e.getErrorCode() == 1062) {
throw new EntityExistsException("Entity already exists ", e);
}
throw new CloudRuntimeException("DB Exception on: " + pstmt, e);
}
}
use of javax.persistence.EntityExistsException in project CloudStack-archive by CloudStack-extras.
the class GenericDaoBase method persist.
@Override
@SuppressWarnings("unchecked")
public T persist(final T entity) {
if (Enhancer.isEnhanced(entity.getClass())) {
if (_idField != null) {
ID id;
try {
id = (ID) _idField.get(entity);
} catch (IllegalAccessException e) {
throw new CloudRuntimeException("How can it be illegal access...come on", e);
}
update(id, entity);
return entity;
}
assert false : "Can't call persit if you don't have primary key";
}
ID id = null;
final Transaction txn = Transaction.currentTxn();
PreparedStatement pstmt = null;
String sql = null;
try {
txn.start();
for (final Pair<String, Attribute[]> pair : _insertSqls) {
sql = pair.first();
final Attribute[] attrs = pair.second();
pstmt = txn.prepareAutoCloseStatement(sql, Statement.RETURN_GENERATED_KEYS);
int index = 1;
index = prepareAttributes(pstmt, entity, attrs, index);
pstmt.executeUpdate();
final ResultSet rs = pstmt.getGeneratedKeys();
if (id == null) {
if (rs != null && rs.next()) {
id = (ID) rs.getObject(1);
}
try {
if (_idField != null) {
if (id != null) {
_idField.set(entity, id);
} else {
id = (ID) _idField.get(entity);
}
}
} catch (final IllegalAccessException e) {
throw new CloudRuntimeException("Yikes! ", e);
}
}
}
if (_ecAttributes != null && _ecAttributes.size() > 0) {
HashMap<Attribute, Object> ecAttributes = new HashMap<Attribute, Object>();
for (Attribute attr : _ecAttributes) {
Object ec = attr.field.get(entity);
if (ec != null) {
ecAttributes.put(attr, ec);
}
}
insertElementCollection(entity, _idAttributes.get(_table)[0], id, ecAttributes);
}
txn.commit();
} catch (final SQLException e) {
if (e.getSQLState().equals("23000") && e.getErrorCode() == 1062) {
throw new EntityExistsException("Entity already exists: ", e);
} else {
throw new CloudRuntimeException("DB Exception on: " + pstmt, e);
}
} catch (IllegalArgumentException e) {
throw new CloudRuntimeException("Problem with getting the ec attribute ", e);
} catch (IllegalAccessException e) {
throw new CloudRuntimeException("Problem with getting the ec attribute ", e);
}
return _idField != null ? findByIdIncludingRemoved(id) : null;
}
use of javax.persistence.EntityExistsException in project spring-framework by spring-projects.
the class EntityManagerFactoryUtilsTests method testConvertJpaPersistenceException.
/*
* Test method for
* 'org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessException(PersistenceException)'
*/
@Test
@SuppressWarnings("serial")
public void testConvertJpaPersistenceException() {
EntityNotFoundException entityNotFound = new EntityNotFoundException();
assertSame(JpaObjectRetrievalFailureException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(entityNotFound).getClass());
NoResultException noResult = new NoResultException();
assertSame(EmptyResultDataAccessException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(noResult).getClass());
NonUniqueResultException nonUniqueResult = new NonUniqueResultException();
assertSame(IncorrectResultSizeDataAccessException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(nonUniqueResult).getClass());
OptimisticLockException optimisticLock = new OptimisticLockException();
assertSame(JpaOptimisticLockingFailureException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(optimisticLock).getClass());
EntityExistsException entityExists = new EntityExistsException("foo");
assertSame(DataIntegrityViolationException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(entityExists).getClass());
TransactionRequiredException transactionRequired = new TransactionRequiredException("foo");
assertSame(InvalidDataAccessApiUsageException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(transactionRequired).getClass());
PersistenceException unknown = new PersistenceException() {
};
assertSame(JpaSystemException.class, EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(unknown).getClass());
}
Aggregations