Search in sources :

Example 11 with EntityExistsException

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);
    }
}
Also used : EntityExistsException(javax.persistence.EntityExistsException) ServiceException(gov.ca.cwds.rest.services.ServiceException) PostedAllegation(gov.ca.cwds.rest.api.domain.cms.PostedAllegation) Allegation(gov.ca.cwds.data.persistence.cms.Allegation) PostedAllegation(gov.ca.cwds.rest.api.domain.cms.PostedAllegation)

Example 12 with EntityExistsException

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;
    }
}
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 13 with EntityExistsException

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);
    }
}
Also used : Ternary(com.cloud.utils.Ternary) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) EntityExistsException(javax.persistence.EntityExistsException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 14 with EntityExistsException

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;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) EntityExistsException(javax.persistence.EntityExistsException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResultSet(java.sql.ResultSet) UUID(java.util.UUID) AttributeOverride(javax.persistence.AttributeOverride)

Example 15 with EntityExistsException

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());
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException) TransactionRequiredException(javax.persistence.TransactionRequiredException) PersistenceException(javax.persistence.PersistenceException) OptimisticLockException(javax.persistence.OptimisticLockException) EntityNotFoundException(javax.persistence.EntityNotFoundException) NoResultException(javax.persistence.NoResultException) EntityExistsException(javax.persistence.EntityExistsException) Test(org.junit.Test)

Aggregations

EntityExistsException (javax.persistence.EntityExistsException)24 ServiceException (gov.ca.cwds.rest.services.ServiceException)13 SQLException (java.sql.SQLException)5 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)4 StaffPerson (gov.ca.cwds.data.persistence.cms.StaffPerson)4 PreparedStatement (java.sql.PreparedStatement)4 DB (com.cloud.utils.db.DB)3 PersistenceException (javax.persistence.PersistenceException)3 Ternary (com.cloud.utils.Ternary)2 ResultSet (java.sql.ResultSet)2 HashMap (java.util.HashMap)2 UUID (java.util.UUID)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 AttributeOverride (javax.persistence.AttributeOverride)2 EntityNotFoundException (javax.persistence.EntityNotFoundException)2 NoResultException (javax.persistence.NoResultException)2 NonUniqueResultException (javax.persistence.NonUniqueResultException)2 OptimisticLockException (javax.persistence.OptimisticLockException)2 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 CiscoAsa1000vDevice (com.cloud.network.cisco.CiscoAsa1000vDevice)1