List of usage examples for org.springframework.dao DuplicateKeyException DuplicateKeyException
public DuplicateKeyException(String msg, Throwable cause)
From source file:org.seasar.doma.boot.autoconfigure.DomaPersistenceExceptionTranslator.java
@Override public DataAccessException translateExceptionIfPossible(RuntimeException ex) { if (!(ex instanceof JdbcException)) { // Fallback to other translators if not JdbcException return null; }//w w w .ja va2s . c o m if (ex instanceof OptimisticLockException) { return new OptimisticLockingFailureException(ex.getMessage(), ex); } else if (ex instanceof UniqueConstraintException) { return new DuplicateKeyException(ex.getMessage(), ex); } else if (ex instanceof NonUniqueResultException || ex instanceof NonSingleColumnException) { return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex); } else if (ex instanceof NoResultException) { return new EmptyResultDataAccessException(ex.getMessage(), 1, ex); } else if (ex instanceof UnknownColumnException || ex instanceof ResultMappingException) { return new TypeMismatchDataAccessException(ex.getMessage(), ex); } if (ex.getCause() instanceof SQLException) { SQLException e = (SQLException) ex.getCause(); String sql = null; if (ex instanceof SqlExecutionException) { sql = ((SqlExecutionException) ex).getRawSql(); } return translator.translate(ex.getMessage(), sql, e); } return new UncategorizedDataAccessException(ex.getMessage(), ex) { }; }
From source file:org.exoplatform.acceptance.rest.administration.vcs.VCSRepositoryCRUDControllerTest.java
@Test public void add_NameAlreadyExists_ShouldReturnValidationError() throws Exception { VCSRepository objectToCreate = new VCSRepository(Strings.padEnd("a", 10, 'a')); objectToCreate.addRemoteRepository("acceptance", "git@github.com:exoplatform/acceptance.git"); String exMsg = "{ \"serverUsed\" : \"localhost/127.0.0.1:27017\" , \"err\" : \"E11000 duplicate key error index: acceptance" + ".credentials.$name dup key: { : \\\"" + objectToCreate.getName() + "\\\" }\" , \"code\" : 11000 , \"n\" : 0 , \"connectionId\" : 5 , \"ok\" : 1.0}"; String errorMsg = "E11000 duplicate key error index: acceptance.credentials.$name dup key: { : \"" + objectToCreate.getName() + "\" }"; when(vcsRepositoryServiceMock.updateOrCreate(any(VCSRepository.class))) .thenThrow(new DuplicateKeyException("", new Exception(exMsg))); mockMvc.perform(post("/admin/vcs/repository").contentType(MediaType.APPLICATION_JSON) .content(convertObjectToJsonBytes(objectToCreate))).andExpect(status().isConflict()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.globalErrors", hasSize(1))) .andExpect(jsonPath("$.globalErrors[*].objectName", containsInAnyOrder("Duplicated Key Error"))) .andExpect(jsonPath("$.globalErrors[*].message", containsInAnyOrder(errorMsg))); ArgumentCaptor<VCSRepository> dtoCaptor = ArgumentCaptor.forClass(VCSRepository.class); verify(vcsRepositoryServiceMock, times(1)).updateOrCreate(dtoCaptor.capture()); verifyNoMoreInteractions(vcsRepositoryServiceMock); VCSRepository dtoArgument = dtoCaptor.getValue(); assertNull(dtoArgument.getId());// w w w . j a v a2 s .c om assertThat(dtoArgument.getName(), is(objectToCreate.getName())); assertThat(dtoArgument.getType(), is(objectToCreate.getType())); }
From source file:org.exoplatform.acceptance.rest.administration.credential.CredentialCRUDControllerTest.java
@Test public void add_NameAlreadyExists_ShouldReturnValidationError() throws Exception { TokenCredential objectToCreate = new TokenCredential(Strings.padEnd("a", 10, 'a'), Strings.padEnd("b", 10, 'b')); String exMsg = "{ \"serverUsed\" : \"localhost/127.0.0.1:27017\" , \"err\" : \"E11000 duplicate key error index: acceptance" + ".credentials.$name dup key: { : \\\"" + objectToCreate.getName() + "\\\" }\" , \"code\" : 11000 , \"n\" : 0 , \"connectionId\" : 5 , \"ok\" : 1.0}"; String errorMsg = "E11000 duplicate key error index: acceptance.credentials.$name dup key: { : \"" + objectToCreate.getName() + "\" }"; when(credentialServiceMock.updateOrCreate(any(Credential.class))) .thenThrow(new DuplicateKeyException("", new Exception(exMsg))); mockMvc.perform(post("/admin/credential").contentType(MediaType.APPLICATION_JSON) .content(convertObjectToJsonBytes(objectToCreate))).andExpect(status().isConflict()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.globalErrors", hasSize(1))) .andExpect(jsonPath("$.globalErrors[*].objectName", containsInAnyOrder("Duplicated Key Error"))) .andExpect(jsonPath("$.globalErrors[*].message", containsInAnyOrder(errorMsg))); ArgumentCaptor<Credential> dtoCaptor = ArgumentCaptor.forClass(Credential.class); verify(credentialServiceMock, times(1)).updateOrCreate(dtoCaptor.capture()); verifyNoMoreInteractions(credentialServiceMock); Credential dtoArgument = dtoCaptor.getValue(); assertNull(dtoArgument.getId());/*from ww w .j a va2 s . c o m*/ assertThat(dtoArgument.getName(), is(objectToCreate.getName())); assertThat(dtoArgument.getType(), is(objectToCreate.getType())); }
From source file:jp.classmethod.aws.dynamodb.DynamoDbRepository.java
@Override public <S extends E> S create(S domain) { if (domain == null) { return null; }//from www . jav a2 s . c o m final Item domainItem = convertDomainToItem(domain); // stackoverflow.com/questions/4460580/java-generics-why-someobject-getclass-doesnt-return-class-extends-t @SuppressWarnings("unchecked") final Class<? extends S> domainClass = (Class<? extends S>) domain.getClass(); Item itemCreated = DynamoDbInternalUtils.cloneItem(domainItem, true /*filterEmptyStrings*/); PutItemSpec spec = putItemSpec(itemCreated); try { table.putItem(spec); } catch (AmazonClientException e) { throw convertDynamoDBException(e, "create", () -> new DuplicateKeyException("uuid " + getId(domain) + " already exists", e)); } return convertItemToDomain(itemCreated, domainClass); }
From source file:org.springframework.orm.hibernate3.SessionFactoryUtils.java
/** * Convert the given HibernateException to an appropriate exception * from the {@code org.springframework.dao} hierarchy. * @param ex HibernateException that occurred * @return the corresponding DataAccessException instance * @see HibernateAccessor#convertHibernateAccessException * @see HibernateTransactionManager#convertHibernateAccessException *//*from w ww. j a v a 2 s .c om*/ public static DataAccessException convertHibernateAccessException(HibernateException ex) { if (ex instanceof JDBCConnectionException) { return new DataAccessResourceFailureException(ex.getMessage(), ex); } if (ex instanceof SQLGrammarException) { SQLGrammarException jdbcEx = (SQLGrammarException) ex; return new InvalidDataAccessResourceUsageException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof QueryTimeoutException) { QueryTimeoutException jdbcEx = (QueryTimeoutException) ex; return new org.springframework.dao.QueryTimeoutException( ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof LockAcquisitionException) { LockAcquisitionException jdbcEx = (LockAcquisitionException) ex; return new CannotAcquireLockException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof PessimisticLockException) { PessimisticLockException jdbcEx = (PessimisticLockException) ex; return new PessimisticLockingFailureException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof ConstraintViolationException) { ConstraintViolationException jdbcEx = (ConstraintViolationException) ex; return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]; constraint [" + jdbcEx.getConstraintName() + "]", ex); } if (ex instanceof DataException) { DataException jdbcEx = (DataException) ex; return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof JDBCException) { return new HibernateJdbcException((JDBCException) ex); } // end of JDBCException (subclass) handling if (ex instanceof QueryException) { return new HibernateQueryException((QueryException) ex); } if (ex instanceof NonUniqueResultException) { return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex); } if (ex instanceof NonUniqueObjectException) { return new DuplicateKeyException(ex.getMessage(), ex); } if (ex instanceof PropertyValueException) { return new DataIntegrityViolationException(ex.getMessage(), ex); } if (ex instanceof PersistentObjectException) { return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); } if (ex instanceof TransientObjectException) { return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); } if (ex instanceof ObjectDeletedException) { return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); } if (ex instanceof UnresolvableObjectException) { return new HibernateObjectRetrievalFailureException((UnresolvableObjectException) ex); } if (ex instanceof WrongClassException) { return new HibernateObjectRetrievalFailureException((WrongClassException) ex); } if (ex instanceof StaleObjectStateException) { return new HibernateOptimisticLockingFailureException((StaleObjectStateException) ex); } if (ex instanceof StaleStateException) { return new HibernateOptimisticLockingFailureException((StaleStateException) ex); } if (ex instanceof OptimisticLockException) { return new HibernateOptimisticLockingFailureException((OptimisticLockException) ex); } // fallback return new HibernateSystemException(ex); }
From source file:org.springframework.orm.hibernate4.fix.SessionFactoryUtils.java
/** * Convert the given HibernateException to an appropriate exception * from the {@code org.springframework.dao} hierarchy. * @param ex HibernateException that occured * @return the corresponding DataAccessException instance * @see HibernateExceptionTranslator#convertHibernateAccessException * @see HibernateTransactionManager#convertHibernateAccessException *//*w ww . j av a 2s .c o m*/ public static DataAccessException convertHibernateAccessException(HibernateException ex) { if (ex instanceof JDBCConnectionException) { return new DataAccessResourceFailureException(ex.getMessage(), ex); } if (ex instanceof SQLGrammarException) { SQLGrammarException jdbcEx = (SQLGrammarException) ex; return new InvalidDataAccessResourceUsageException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof LockAcquisitionException) { LockAcquisitionException jdbcEx = (LockAcquisitionException) ex; return new CannotAcquireLockException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof ConstraintViolationException) { ConstraintViolationException jdbcEx = (ConstraintViolationException) ex; return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]; constraint [" + jdbcEx.getConstraintName() + "]", ex); } if (ex instanceof DataException) { DataException jdbcEx = (DataException) ex; return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof JDBCException) { return new HibernateJdbcException((JDBCException) ex); } // end of JDBCException (subclass) handling if (ex instanceof QueryException) { return new HibernateQueryException((QueryException) ex); } if (ex instanceof NonUniqueResultException) { return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex); } if (ex instanceof NonUniqueObjectException) { return new DuplicateKeyException(ex.getMessage(), ex); } if (ex instanceof PropertyValueException) { return new DataIntegrityViolationException(ex.getMessage(), ex); } if (ex instanceof PersistentObjectException) { return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); } if (ex instanceof TransientObjectException) { return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); } if (ex instanceof ObjectDeletedException) { return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); } if (ex instanceof UnresolvableObjectException) { return new HibernateObjectRetrievalFailureException((UnresolvableObjectException) ex); } if (ex instanceof WrongClassException) { return new HibernateObjectRetrievalFailureException((WrongClassException) ex); } if (ex instanceof StaleObjectStateException) { return new HibernateOptimisticLockingFailureException((StaleObjectStateException) ex); } if (ex instanceof StaleStateException) { return new HibernateOptimisticLockingFailureException((StaleStateException) ex); } // fallback return new HibernateSystemException(ex); }
From source file:org.springframework.orm.hibernate4.SessionFactoryUtils.java
/** * Convert the given HibernateException to an appropriate exception * from the {@code org.springframework.dao} hierarchy. * @param ex HibernateException that occurred * @return the corresponding DataAccessException instance * @see HibernateExceptionTranslator#convertHibernateAccessException * @see HibernateTransactionManager#convertHibernateAccessException *//* w w w. j a v a 2s. c o m*/ public static DataAccessException convertHibernateAccessException(HibernateException ex) { if (ex instanceof JDBCConnectionException) { return new DataAccessResourceFailureException(ex.getMessage(), ex); } if (ex instanceof SQLGrammarException) { SQLGrammarException jdbcEx = (SQLGrammarException) ex; return new InvalidDataAccessResourceUsageException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof QueryTimeoutException) { QueryTimeoutException jdbcEx = (QueryTimeoutException) ex; return new org.springframework.dao.QueryTimeoutException( ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof LockAcquisitionException) { LockAcquisitionException jdbcEx = (LockAcquisitionException) ex; return new CannotAcquireLockException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof PessimisticLockException) { PessimisticLockException jdbcEx = (PessimisticLockException) ex; return new PessimisticLockingFailureException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof ConstraintViolationException) { ConstraintViolationException jdbcEx = (ConstraintViolationException) ex; return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]; constraint [" + jdbcEx.getConstraintName() + "]", ex); } if (ex instanceof DataException) { DataException jdbcEx = (DataException) ex; return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof JDBCException) { return new HibernateJdbcException((JDBCException) ex); } // end of JDBCException (subclass) handling if (ex instanceof QueryException) { return new HibernateQueryException((QueryException) ex); } if (ex instanceof NonUniqueResultException) { return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex); } if (ex instanceof NonUniqueObjectException) { return new DuplicateKeyException(ex.getMessage(), ex); } if (ex instanceof PropertyValueException) { return new DataIntegrityViolationException(ex.getMessage(), ex); } if (ex instanceof PersistentObjectException) { return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); } if (ex instanceof TransientObjectException) { return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); } if (ex instanceof ObjectDeletedException) { return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); } if (ex instanceof UnresolvableObjectException) { return new HibernateObjectRetrievalFailureException((UnresolvableObjectException) ex); } if (ex instanceof WrongClassException) { return new HibernateObjectRetrievalFailureException((WrongClassException) ex); } if (ex instanceof StaleObjectStateException) { return new HibernateOptimisticLockingFailureException((StaleObjectStateException) ex); } if (ex instanceof StaleStateException) { return new HibernateOptimisticLockingFailureException((StaleStateException) ex); } if (ex instanceof OptimisticEntityLockException) { return new HibernateOptimisticLockingFailureException((OptimisticEntityLockException) ex); } if (ex instanceof PessimisticEntityLockException) { if (ex.getCause() instanceof LockAcquisitionException) { return new CannotAcquireLockException(ex.getMessage(), ex.getCause()); } return new PessimisticLockingFailureException(ex.getMessage(), ex); } // fallback return new HibernateSystemException(ex); }
From source file:org.springframework.orm.jpa.vendor.HibernateJpaDialect.java
/** * Convert the given HibernateException to an appropriate exception * from the {@code org.springframework.dao} hierarchy. * @param ex HibernateException that occurred * @return the corresponding DataAccessException instance */// w ww . j a v a 2s .com protected DataAccessException convertHibernateAccessException(HibernateException ex) { if (ex instanceof JDBCConnectionException) { return new DataAccessResourceFailureException(ex.getMessage(), ex); } if (ex instanceof SQLGrammarException) { SQLGrammarException jdbcEx = (SQLGrammarException) ex; return new InvalidDataAccessResourceUsageException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof QueryTimeoutException) { QueryTimeoutException jdbcEx = (QueryTimeoutException) ex; return new org.springframework.dao.QueryTimeoutException( ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof LockAcquisitionException) { LockAcquisitionException jdbcEx = (LockAcquisitionException) ex; return new CannotAcquireLockException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof PessimisticLockException) { PessimisticLockException jdbcEx = (PessimisticLockException) ex; return new PessimisticLockingFailureException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } if (ex instanceof ConstraintViolationException) { ConstraintViolationException jdbcEx = (ConstraintViolationException) ex; return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]; constraint [" + jdbcEx.getConstraintName() + "]", ex); } if (ex instanceof DataException) { DataException jdbcEx = (DataException) ex; return new DataIntegrityViolationException(ex.getMessage() + "; SQL [" + jdbcEx.getSQL() + "]", ex); } // end of JDBCException subclass handling if (ex instanceof QueryException) { return new InvalidDataAccessResourceUsageException(ex.getMessage(), ex); } if (ex instanceof NonUniqueResultException) { return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex); } if (ex instanceof NonUniqueObjectException) { return new DuplicateKeyException(ex.getMessage(), ex); } if (ex instanceof PropertyValueException) { return new DataIntegrityViolationException(ex.getMessage(), ex); } if (ex instanceof PersistentObjectException) { return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); } if (ex instanceof TransientObjectException) { return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); } if (ex instanceof ObjectDeletedException) { return new InvalidDataAccessApiUsageException(ex.getMessage(), ex); } if (ex instanceof UnresolvableObjectException) { UnresolvableObjectException hibEx = (UnresolvableObjectException) ex; return new ObjectRetrievalFailureException(hibEx.getEntityName(), hibEx.getIdentifier(), ex.getMessage(), ex); } if (ex instanceof WrongClassException) { WrongClassException hibEx = (WrongClassException) ex; return new ObjectRetrievalFailureException(hibEx.getEntityName(), hibEx.getIdentifier(), ex.getMessage(), ex); } if (ex instanceof StaleObjectStateException) { StaleObjectStateException hibEx = (StaleObjectStateException) ex; return new ObjectOptimisticLockingFailureException(hibEx.getEntityName(), hibEx.getIdentifier(), ex); } if (ex instanceof StaleStateException) { return new ObjectOptimisticLockingFailureException(ex.getMessage(), ex); } if (ex instanceof OptimisticEntityLockException) { return new ObjectOptimisticLockingFailureException(ex.getMessage(), ex); } if (ex instanceof PessimisticEntityLockException) { if (ex.getCause() instanceof LockAcquisitionException) { return new CannotAcquireLockException(ex.getMessage(), ex.getCause()); } return new PessimisticLockingFailureException(ex.getMessage(), ex); } // fallback return new JpaSystemException(ex); }