类org.hibernate.exception.ConstraintViolationException源码实例Demo

下面列出了怎么用org.hibernate.exception.ConstraintViolationException的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: judgels   文件: UserStoreIntegrationTests.java
@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));
}
 
源代码2 项目: crnk-framework   文件: JpaExceptionMapperTests.java
@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());
}
 
/**
 * 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
}
 
源代码4 项目: lams   文件: SybaseASE157Dialect.java
@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;
		}
	};
}
 
@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();
}
 
源代码6 项目: abixen-platform   文件: DataSourceController.java
@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);
}
 
/**
 * 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;
}
 
源代码8 项目: zstack   文件: AccountBase.java
@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);
}
 
源代码9 项目: zstack   文件: AccountBase.java
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);
}
 
源代码10 项目: olat   文件: TransactionRetryerITCaseNew.java
@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);

}
 
源代码11 项目: olat   文件: TransactionRetryerITCaseNew.java
@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));
}
 
源代码12 项目: olat   文件: TransactionRetryerITCaseNew.java
@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));
}
 
源代码13 项目: cloudbreak   文件: BlueprintService.java
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);
    }
}
 
源代码14 项目: yeti   文件: SQLiteDialect.java
@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);
        }
    };
}
 
@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();
	}
}
 
@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();
	}
}
 
源代码17 项目: judgels   文件: SessionStoreIntegrationTests.java
@Test
void token_has_unique_constraint() {
    store.createSession("token123", "userJid1");

    assertThatExceptionOfType(ConstraintViolationException.class)
            .isThrownBy(() -> store.createSession("token123", "userJid2"));
}
 
@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()));
}
 
@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;
    }
  }
}
 
/**
 * 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);
}
 
/**
 * 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));
}
 
@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();
}
 
@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);
}
 
源代码24 项目: lams   文件: SQLExceptionTypeDelegate.java
@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
}
 
源代码25 项目: keycloak   文件: PersistenceExceptionConverter.java
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);
    }
}
 
/**
 * 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();
		}
	}
}
 
源代码27 项目: spring-boot   文件: RestExceptionHandler.java
/**
 * 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);
}
 
源代码28 项目: spring-boot   文件: RestExceptionHandler.java
/**
 * 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));
}
 
源代码29 项目: abixen-platform   文件: RoleService.java
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;
        }
    }
}
 
源代码30 项目: tutorials   文件: HibernateExceptionUnitTest.java
@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);
        }
    }
}
 
 类所在包
 同包方法