org.hibernate.exception.ConstraintViolationException Java Examples

The following examples show how to use org.hibernate.exception.ConstraintViolationException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: judgels   Author: ia-toki   File: UserStoreIntegrationTests.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Test
void email_has_unique_constraint() {
    UserData userData = new UserData.Builder()
            .username("username")
            .password("password")
            .email("[email protected]")
            .build();
    store.createUser(userData);

    UserData newUserData = new UserData.Builder()
            .username("new.username")
            .password("new.password")
            .email("[email protected]")
            .build();
    assertThatExceptionOfType(ConstraintViolationException.class)
            .isThrownBy(() -> store.createUser(newUserData));
}
 
Example #2
Source Project: crnk-framework   Author: crnk-project   File: JpaExceptionMapperTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testConstraintException() {
    ConstraintViolationException exception = new ConstraintViolationException("message", null, "constraint");
    ExceptionMapperRegistry exceptionMapperRegistry = boot.getExceptionMapperRegistry();
    HibernateConstraintViolationExceptionMapper mapper = (HibernateConstraintViolationExceptionMapper) exceptionMapperRegistry.findMapperFor(ConstraintViolationException.class).get();
    ErrorResponse response = mapper.toErrorResponse(exception);
    ErrorData errorData = response.getErrors().iterator().next();
    Assert.assertEquals(Integer.toString(HttpStatus.UNPROCESSABLE_ENTITY_422), errorData.getStatus());
    Assert.assertEquals(exception.getConstraintName(), errorData.getCode());
    Assert.assertEquals(exception.getMessage(), errorData.getDetail());

    Assert.assertTrue(mapper.accepts(response));
    ConstraintViolationException deserializedException = mapper.fromErrorResponse(response);
    Assert.assertEquals(exception.getMessage(), deserializedException.getMessage());
    Assert.assertEquals(exception.getConstraintName(), deserializedException.getConstraintName());
}
 
Example #3
Source Project: lams   Author: lamsfoundation   File: CacheSQLExceptionConversionDelegate.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Convert the given SQLException into Hibernate's JDBCException hierarchy.
 *
 * @param sqlException The SQLException to be converted.
 * @param message	  An optional error message.
 * @param sql		  Optionally, the sql being performed when the exception occurred.
 * @return The resulting JDBCException; returns null if it could not be converted.
 */
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
	String sqlStateClassCode = JdbcExceptionHelper.extractSqlStateClassCode( sqlException );
	if ( sqlStateClassCode != null ) {
		Integer errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
		if ( INTEGRITY_VIOLATION_CATEGORIES.contains( errorCode ) ) {
			String constraintName =
					getConversionContext()
							.getViolatedConstraintNameExtracter()
							.extractConstraintName( sqlException );
			return new ConstraintViolationException( message, sqlException, sql, constraintName );
		}
		else if ( DATA_CATEGORIES.contains( sqlStateClassCode ) ) {
			return new DataException( message, sqlException, sql );
		}
	}
	return null; // allow other delegates the chance to look
}
 
Example #4
Source Project: lams   Author: lamsfoundation   File: SybaseASE157Dialect.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public SQLExceptionConversionDelegate buildSQLExceptionConversionDelegate() {
	return new SQLExceptionConversionDelegate() {
		@Override
		public JDBCException convert(SQLException sqlException, String message, String sql) {
			final String sqlState = JdbcExceptionHelper.extractSqlState( sqlException );
			final int errorCode = JdbcExceptionHelper.extractErrorCode( sqlException );
			if("JZ0TO".equals( sqlState ) || "JZ006".equals( sqlState )){
				throw new LockTimeoutException( message, sqlException, sql );
			}
			if ( 515 == errorCode && "ZZZZZ".equals( sqlState ) ) {
				// Attempt to insert NULL value into column; column does not allow nulls.
				final String constraintName = getViolatedConstraintNameExtracter().extractConstraintName( sqlException );
				return new ConstraintViolationException( message, sqlException, sql, constraintName );
			}
			return null;
		}
	};
}
 
Example #5
Source Project: spring4-understanding   Author: langtianya   File: HibernateInterceptorTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testInterceptorWithFlushFailure() throws Throwable {
	SQLException sqlEx = new SQLException("argh", "27");
	ConstraintViolationException jdbcEx = new ConstraintViolationException("", sqlEx, null);
	willThrow(jdbcEx).given(session).flush();

	HibernateInterceptor interceptor = new HibernateInterceptor();
	interceptor.setSessionFactory(sessionFactory);
	try {
		interceptor.invoke(invocation);
		fail("Should have thrown DataIntegrityViolationException");
	}
	catch (DataIntegrityViolationException ex) {
		// expected
		assertEquals(jdbcEx, ex.getCause());
	}

	verify(session).close();
}
 
Example #6
Source Project: abixen-platform   Author: abixen   File: DataSourceController.java    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public FormValidationResultRepresentation<DataSourceForm> updateDataSource(@PathVariable("id") Long id, @RequestBody @Valid DataSourceForm dataSourceForm, BindingResult bindingResult) {
    log.debug("updateDataSource() - dataSourceForm: {}, bindingResult: {}", dataSourceForm, bindingResult);

    if (bindingResult.hasErrors()) {
        List<FormErrorRepresentation> formErrors = ValidationUtil.extractFormErrors(bindingResult);
        return new FormValidationResultRepresentation<>(dataSourceForm, formErrors);
    }

    //FIXME - move to domain
    try {
        dataSourceManagementService.updateDataSource(dataSourceForm);
    } catch (Throwable e) {
        log.error(e.getMessage());
        if (e.getCause() instanceof ConstraintViolationException) {
            throw new PlatformRuntimeException("Data source can not be updated. If you want to change available columns then you need to detach they from module firstly.");
        } else {
            throw e;
        }
    }
    return new FormValidationResultRepresentation<>(dataSourceForm);
}
 
Example #7
Source Project: herd   Author: FINRAOS   File: HerdErrorInformationExceptionHandler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns {@code true} if the given throwable is or is not caused by a database constraint violation.
 *
 * @param exception - throwable to check.
 *
 * @return {@code true} if is constraint violation, {@code false} otherwise.
 */
private boolean isCausedByConstraintViolationException(Exception exception)
{
    // some databases will throw ConstraintViolationException
    boolean isConstraintViolation = ExceptionUtils.indexOfThrowable(exception, ConstraintViolationException.class) != -1;

    // other databases will not throw a nice exception
    if (!isConstraintViolation)
    {
        // We must manually check the error codes
        Throwable rootThrowable = getRootCause(exception);
        if (rootThrowable instanceof SQLException)
        {
            SQLException sqlException = (SQLException) rootThrowable;
            if (POSTGRES_SQL_STATE_CODE_FOREIGN_KEY_VIOLATION.equals(sqlException.getSQLState())
                || POSTGRES_SQL_STATE_CODE_UNIQUE_INDEX_OR_PRIMARY_KEY_VIOLATION.equals(sqlException.getSQLState()))
            {
                isConstraintViolation = true;
            }
        }
    }
    return isConstraintViolation;
}
 
Example #8
Source Project: zstack   Author: zstackio   File: AccountBase.java    License: Apache License 2.0 6 votes vote down vote up
@Transactional
private void handle(APIAttachPoliciesToUserMsg msg) {
    for (String puuid : msg.getPolicyUuids()) {
        try {
            UserPolicyRefVO refVO = new UserPolicyRefVO();
            refVO.setUserUuid(msg.getUserUuid());
            refVO.setPolicyUuid(puuid);
            dbf.getEntityManager().persist(refVO);
            dbf.getEntityManager().flush();
        } catch (Throwable t) {
            if (!ExceptionDSL.isCausedBy(t, ConstraintViolationException.class)) {
                throw t;
            }

            // the policy is already attached
        }
    }

    APIAttachPoliciesToUserEvent evt = new APIAttachPoliciesToUserEvent(msg.getId());
    bus.publish(evt);
}
 
Example #9
Source Project: zstack   Author: zstackio   File: AccountBase.java    License: Apache License 2.0 6 votes vote down vote up
private void handle(APIAttachPolicyToUserGroupMsg msg) {
    UserGroupPolicyRefVO grvo = new UserGroupPolicyRefVO();
    grvo.setGroupUuid(msg.getGroupUuid());
    grvo.setPolicyUuid(msg.getPolicyUuid());

    try {
        dbf.persist(grvo);
    } catch (Throwable t) {
        if (!ExceptionDSL.isCausedBy(t, ConstraintViolationException.class)) {
            throw t;
        }

        // the policy is already attached
    }

    APIAttachPolicyToUserGroupEvent evt = new APIAttachPolicyToUserGroupEvent(msg.getId());
    bus.publish(evt);
}
 
Example #10
Source Project: olat   Author: huihoo   File: TransactionRetryerITCaseNew.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSubscribe_IfCoreServiceThrowsException_checkNumberOfRetries() {

    NotificationService mockNotificationService = mock(NotificationService.class);
    when(mockNotificationService.subscribe(notificationSubscriptionContext1)).thenThrow(new ConstraintViolationException(IDENTITY_NAME_1, null, IDENTITY_NAME_1));
    learnServiceImpl.setNotificationService(mockNotificationService);

    try {
        verify(learnServiceImpl.subscribe(notificationSubscriptionContext1));
    } catch (Exception e) {
        System.out.println("catch to check for number of retries");
    }
    // the number of retries for ConstraintViolationException is configured via maxRetriesPerException bean property in lmsLearnTestContext.xml
    verify(mockNotificationService, times(2)).subscribe(notificationSubscriptionContext1);

}
 
Example #11
Source Project: olat   Author: huihoo   File: TransactionRetryerITCaseNew.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void isRetryStillAllowed_oneExceptionType_oneRetryAllowed() {
    Map<String, Long> retriesPerException = new HashMap<String, Long>();
    assertTrue(transactionRetryer.isRetryStillAllowed(retriesPerException));

    // retry once allowed
    transactionRetryer.addOrIncrementRetries(retriesPerException, ConstraintViolationException.class.getName());
    assertTrue(transactionRetryer.isRetryStillAllowed(retriesPerException));

    // retry second time no more allowed
    transactionRetryer.addOrIncrementRetries(retriesPerException, ConstraintViolationException.class.getName());
    assertFalse(transactionRetryer.isRetryStillAllowed(retriesPerException));

    // retry no more allowed
    transactionRetryer.addOrIncrementRetries(retriesPerException, ConstraintViolationException.class.getName());
    assertFalse(transactionRetryer.isRetryStillAllowed(retriesPerException));
}
 
Example #12
Source Project: olat   Author: huihoo   File: TransactionRetryerITCaseNew.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void isRetryStillAllowed_oneExceptionType_oneRetryAllowed() {
    Map<String, Long> retriesPerException = new HashMap<String, Long>();
    assertTrue(transactionRetryer.isRetryStillAllowed(retriesPerException));

    // retry once allowed
    transactionRetryer.addOrIncrementRetries(retriesPerException, ConstraintViolationException.class.getName());
    assertTrue(transactionRetryer.isRetryStillAllowed(retriesPerException));

    // retry second time no more allowed
    transactionRetryer.addOrIncrementRetries(retriesPerException, ConstraintViolationException.class.getName());
    assertFalse(transactionRetryer.isRetryStillAllowed(retriesPerException));

    // retry no more allowed
    transactionRetryer.addOrIncrementRetries(retriesPerException, ConstraintViolationException.class.getName());
    assertFalse(transactionRetryer.isRetryStillAllowed(retriesPerException));
}
 
Example #13
Source Project: cloudbreak   Author: hortonworks   File: BlueprintService.java    License: Apache License 2.0 6 votes vote down vote up
private Set<Blueprint> updateDefaultBlueprintCollection(Workspace workspace) {
    Set<Blueprint> blueprintsInDatabase = blueprintRepository.findAllByWorkspaceIdAndStatusIn(workspace.getId(),
            Set.of(DEFAULT, DEFAULT_DELETED, USER_MANAGED));
    if (!blueprintLoaderService.isAddingDefaultBlueprintsNecessaryForTheUser(blueprintsInDatabase)) {
        if (blueprintLoaderService.defaultBlueprintDoesNotExistInTheCache(blueprintsInDatabase)) {
            blueprintLoaderService.deleteOldDefaults(blueprintsInDatabase);
        }
        return blueprintsInDatabase.stream()
                .filter(bp -> DEFAULT.equals(bp.getStatus()) || DEFAULT_DELETED.equals(bp.getStatus()))
                .collect(Collectors.toSet());
    }
    LOGGER.debug("Modifying blueprints based on the defaults for the '{}' workspace.", workspace.getId());
    try {
        Set<Blueprint> updatedBlueprints =
                blueprintLoaderService.loadBlueprintsForTheWorkspace(blueprintsInDatabase, workspace, this::saveDefaultsWithReadRight);
        LOGGER.debug("Blueprint modifications finished based on the defaults for '{}' workspace.", workspace.getId());
        return updatedBlueprints;
    } catch (ConstraintViolationException e) {
        return updateDefaultBlueprintCollection(workspace);
    }
}
 
Example #14
Source Project: yeti   Author: sensepost   File: SQLiteDialect.java    License: MIT License 6 votes vote down vote up
@Override
public SQLExceptionConverter buildSQLExceptionConverter() {
    return new SQLExceptionConverter() {
        @Override
        public JDBCException convert(SQLException sqlException, String message, String sql) {
            final int errorCode = sqlException.getErrorCode();
            if (errorCode == SQLITE_CONSTRAINT) {
                final String constraintName = EXTRACTER.extractConstraintName(sqlException);
                return new ConstraintViolationException(message, sqlException, sql, constraintName);
            } else if (errorCode == SQLITE_TOOBIG || errorCode == SQLITE_MISMATCH) {
                return new DataException(message, sqlException, sql);
            } else if (errorCode == SQLITE_BUSY || errorCode == SQLITE_LOCKED) {
                return new LockAcquisitionException(message, sqlException, sql);
            } else if ((errorCode >= SQLITE_IOERR && errorCode <= SQLITE_PROTOCOL) || errorCode == SQLITE_NOTADB) {
                return new JDBCConnectionException(message, sqlException, sql);
            }
            return new GenericJDBCException(message, sqlException, sql);
        }
    };
}
 
Example #15
Source Project: spring-analysis-note   Author: Vip-Augus   File: HibernateSessionFlushingTests.java    License: MIT License 5 votes vote down vote up
@Test(expected = ConstraintViolationException.class)
public void updateSamWithNullDriversLicenseWithSessionFlush() throws Throwable {
	updateSamWithNullDriversLicense();
	// Manual flush is required to avoid false positive in test
	try {
		sessionFactory.getCurrentSession().flush();
	}
	catch (PersistenceException ex) {
		// Wrapped in Hibernate 5.2, with the constraint violation as cause
		throw ex.getCause();
	}
}
 
Example #16
Source Project: java-technology-stack   Author: codeEngraver   File: HibernateSessionFlushingTests.java    License: MIT License 5 votes vote down vote up
@Test(expected = ConstraintViolationException.class)
public void updateSamWithNullDriversLicenseWithSessionFlush() throws Throwable {
	updateSamWithNullDriversLicense();
	// Manual flush is required to avoid false positive in test
	try {
		sessionFactory.getCurrentSession().flush();
	}
	catch (PersistenceException ex) {
		// Wrapped in Hibernate 5.2, with the constraint violation as cause
		throw ex.getCause();
	}
}
 
Example #17
Source Project: judgels   Author: ia-toki   File: SessionStoreIntegrationTests.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Test
void token_has_unique_constraint() {
    store.createSession("token123", "userJid1");

    assertThatExceptionOfType(ConstraintViolationException.class)
            .isThrownBy(() -> store.createSession("token123", "userJid2"));
}
 
Example #18
Source Project: devicehive-java-server   Author: devicehive   File: PersistenceExceptionMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Response toResponse(PersistenceException exception) {
    if (exception.getCause() instanceof ConstraintViolationException) {
        return ResponseFactory
            .response(Response.Status.CONFLICT, new ErrorResponse(Response.Status.CONFLICT.getStatusCode(), Messages.CONFLICT_MESSAGE));
    }
    logger.error("Error: ", exception);
    return ResponseFactory
        .response(Response.Status.INTERNAL_SERVER_ERROR, new ErrorResponse(exception.getMessage()));
}
 
Example #19
Source Project: elide-spring-boot   Author: illyasviel   File: SpringHibernateTransaction.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void flush(RequestScope requestScope) {
  try {
    super.flush(requestScope);
  } catch (TransactionException e) {
    PersistenceException pe = (PersistenceException) e.getCause();
    if (pe.getCause() instanceof ConstraintViolationException) {
      throw new UnprocessableEntityException(
          "Some fields violate constraint(notnull, unique, ...)", e);
    } else {
      throw e;
    }
  }
}
 
Example #20
Source Project: spring-boot-exception-handling   Author: brunocleite   File: RestExceptionHandler.java    License: MIT License 5 votes vote down vote up
/**
 * Handles javax.validation.ConstraintViolationException. Thrown when @Validated fails.
 *
 * @param ex the ConstraintViolationException
 * @return the ApiError object
 */
@ExceptionHandler(javax.validation.ConstraintViolationException.class)
protected ResponseEntity<Object> handleConstraintViolation(
        javax.validation.ConstraintViolationException ex) {
    ApiError apiError = new ApiError(BAD_REQUEST);
    apiError.setMessage("Validation error");
    apiError.addValidationErrors(ex.getConstraintViolations());
    return buildResponseEntity(apiError);
}
 
Example #21
Source Project: spring-boot-exception-handling   Author: brunocleite   File: RestExceptionHandler.java    License: MIT License 5 votes vote down vote up
/**
 * Handle DataIntegrityViolationException, inspects the cause for different DB causes.
 *
 * @param ex the DataIntegrityViolationException
 * @return the ApiError object
 */
@ExceptionHandler(DataIntegrityViolationException.class)
protected ResponseEntity<Object> handleDataIntegrityViolation(DataIntegrityViolationException ex,
                                                              WebRequest request) {
    if (ex.getCause() instanceof ConstraintViolationException) {
        return buildResponseEntity(new ApiError(HttpStatus.CONFLICT, "Database error", ex.getCause()));
    }
    return buildResponseEntity(new ApiError(HttpStatus.INTERNAL_SERVER_ERROR, ex));
}
 
Example #22
Source Project: crnk-framework   Author: crnk-project   File: HibernateConstraintViolationExceptionMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ErrorResponse toErrorResponse(ConstraintViolationException cve) {
	HashMap<String, Object> meta = new HashMap<>();
	meta.put(META_TYPE_KEY, HIBERNATE_CONSTRAINT_VIOLATION_EXCEPTION);

	ErrorData error = ErrorData.builder()
			.setMeta(meta)
			.setStatus(Integer.toString(HttpStatus.UNPROCESSABLE_ENTITY_422))
			.setCode(cve.getConstraintName()).setDetail(cve.getCause() != null ? cve.getCause().getMessage() : cve.getMessage())
			.build();
	return ErrorResponse.builder().setStatus(HttpStatus.UNPROCESSABLE_ENTITY_422).setSingleErrorData(error).build();
}
 
Example #23
Source Project: crnk-framework   Author: crnk-project   File: HibernateConstraintViolationExceptionMapper.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ConstraintViolationException fromErrorResponse(ErrorResponse errorResponse) {
	Iterable<ErrorData> errors = errorResponse.getErrors();
	ErrorData error = errors.iterator().next();
	String msg = error.getDetail();
	String constraintName = error.getCode();
	return new ConstraintViolationException(msg, null, constraintName);
}
 
Example #24
Source Project: lams   Author: lamsfoundation   File: SQLExceptionTypeDelegate.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
	if ( SQLClientInfoException.class.isInstance( sqlException )
			|| SQLInvalidAuthorizationSpecException.class.isInstance( sqlException )
			|| SQLNonTransientConnectionException.class.isInstance( sqlException )
			|| SQLTransientConnectionException.class.isInstance( sqlException ) ) {
		return new JDBCConnectionException( message, sqlException, sql );
	}
	else if ( DataTruncation.class.isInstance( sqlException ) ||
			SQLDataException.class.isInstance( sqlException ) ) {
		throw new DataException( message, sqlException, sql );
	}
	else if ( SQLIntegrityConstraintViolationException.class.isInstance( sqlException ) ) {
		return new ConstraintViolationException(
				message,
				sqlException,
				sql,
				getConversionContext().getViolatedConstraintNameExtracter().extractConstraintName( sqlException )
		);
	}
	else if ( SQLSyntaxErrorException.class.isInstance( sqlException ) ) {
		return new SQLGrammarException( message, sqlException, sql );
	}
	else if ( SQLTimeoutException.class.isInstance( sqlException ) ) {
		return new QueryTimeoutException( message, sqlException, sql );
	}
	else if ( SQLTransactionRollbackException.class.isInstance( sqlException ) ) {
		// Not 100% sure this is completely accurate.  The JavaDocs for SQLTransactionRollbackException state that
		// it indicates sql states starting with '40' and that those usually indicate that:
		//		<quote>
		//			the current statement was automatically rolled back by the database because of deadlock or
		// 			other transaction serialization failures.
		//		</quote>
		return new LockAcquisitionException( message, sqlException, sql );
	}

	return null; // allow other delegates the chance to look
}
 
Example #25
Source Project: keycloak   Author: keycloak   File: PersistenceExceptionConverter.java    License: Apache License 2.0 5 votes vote down vote up
public static ModelException convert(Throwable t) {
    if (t.getCause() != null && t.getCause() instanceof ConstraintViolationException) {
        throw new ModelDuplicateException(t);
    } if (t instanceof EntityExistsException) {
        throw new ModelDuplicateException(t);
    } else {
        throw new ModelException(t);
    }
}
 
Example #26
Source Project: Knowage-Server   Author: KnowageLabs   File: BIObjectParameterDAOHibImpl.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Erase bi object parameter.
 *
 * @param aBIObjectParameter
 *            the a bi object parameter
 *
 * @throws EMFUserError
 *             the EMF user error
 *
 * @see it.eng.spagobi.behaviouralmodel.analyticaldriver.dao.IBIObjectParameterDAO#eraseBIObjectParameter(it.eng.spagobi.behaviouralmodel.analyticaldriver.bo.BIObjectParameter)
 */
@Override
public void eraseBIObjectParameter(BIObjectParameter aBIObjectParameter, boolean alsoDependencies) throws HibernateException {

	Session aSession = null;
	Transaction tx = null;
	try {
		aSession = getSession();
		tx = aSession.beginTransaction();

		eraseBIObjectParameter(aBIObjectParameter, aSession, alsoDependencies);

		tx.commit();
	} catch (ConstraintViolationException e) {
		throw new HibernateException(e.getLocalizedMessage(), e);
	} catch (HibernateException he) {
		logException(he);
		if (tx != null)
			tx.rollback();
		throw new HibernateException(he.getLocalizedMessage(), he);
	} finally {
		if (aSession != null) {
			if (aSession.isOpen())
				aSession.close();
		}
	}
}
 
Example #27
Source Project: spring-boot   Author: h819   File: RestExceptionHandler.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Handles javax.validation.ConstraintViolationException. Thrown when @Validated fails.
 *
 * @param ex the ConstraintViolationException
 * @return the ApiError object
 */
@ExceptionHandler(javax.validation.ConstraintViolationException.class)
protected ResponseEntity<Object> handleConstraintViolation(javax.validation.ConstraintViolationException ex) {
    RestApiError apiError = new RestApiError(BAD_REQUEST);
    apiError.setMessage("Validation error");
    apiError.addValidationErrors(ex.getConstraintViolations());
    return buildResponseEntity(apiError);
}
 
Example #28
Source Project: spring-boot   Author: h819   File: RestExceptionHandler.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Handle DataIntegrityViolationException, inspects the cause for different DB causes.
 *
 * @param ex the DataIntegrityViolationException
 * @return the ApiError object
 */
@ExceptionHandler(DataIntegrityViolationException.class)
protected ResponseEntity<Object> handleDataIntegrityViolation(DataIntegrityViolationException ex,
                                                              WebRequest request) {
    if (ex.getCause() instanceof ConstraintViolationException) {
        return buildResponseEntity(new RestApiError(HttpStatus.CONFLICT, "Database error", ex.getCause()));
    }
    return buildResponseEntity(new RestApiError(HttpStatus.INTERNAL_SERVER_ERROR, ex));
}
 
Example #29
Source Project: abixen-platform   Author: abixen   File: RoleService.java    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
public void delete(final Long id) {
    log.debug("delete() - id: {}", id);

    try {
        roleRepository.delete(id);
    } catch (Throwable e) {
        e.printStackTrace();
        if (e.getCause() instanceof ConstraintViolationException) {
            log.warn("The role id: {} you want to remove is assigned to users.", id);
            throw new PlatformRuntimeException("The role you want to remove is assigned to users.");
        } else {
            throw e;
        }
    }
}
 
Example #30
Source Project: tutorials   Author: eugenp   File: HibernateExceptionUnitTest.java    License: MIT License 5 votes vote down vote up
@Test
public void whenDuplicateIdSaved_thenConstraintViolationException() {
    thrown.expect(isA(PersistenceException.class));
    thrown.expectCause(isA(ConstraintViolationException.class));
    thrown.expectMessage(
        "ConstraintViolationException: could not execute statement");

    Session session = null;
    Transaction transaction = null;

    for (int i = 1; i <= 2; i++) {
        try {
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
            Product product = new Product();
            product.setId(1);
            product.setName("Product " + i);
            session.save(product);
            transaction.commit();
        } catch (Exception e) {
            rollbackTransactionQuietly(transaction);
            throw (e);
        } finally {
            closeSessionQuietly(session);
        }
    }
}