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 File: UserStoreIntegrationTests.java From judgels with GNU General Public License v2.0 | 6 votes |
@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 File: JpaExceptionMapperTests.java From crnk-framework with Apache License 2.0 | 6 votes |
@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 File: CacheSQLExceptionConversionDelegate.java From lams with GNU General Public License v2.0 | 6 votes |
/** * 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 File: SybaseASE157Dialect.java From lams with GNU General Public License v2.0 | 6 votes |
@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 File: HibernateInterceptorTests.java From spring4-understanding with Apache License 2.0 | 6 votes |
@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 File: DataSourceController.java From abixen-platform with GNU Lesser General Public License v2.1 | 6 votes |
@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 File: HerdErrorInformationExceptionHandler.java From herd with Apache License 2.0 | 6 votes |
/** * 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 File: AccountBase.java From zstack with Apache License 2.0 | 6 votes |
@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 File: AccountBase.java From zstack with Apache License 2.0 | 6 votes |
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 File: TransactionRetryerITCaseNew.java From olat with Apache License 2.0 | 6 votes |
@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 File: TransactionRetryerITCaseNew.java From olat with Apache License 2.0 | 6 votes |
@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 File: TransactionRetryerITCaseNew.java From olat with Apache License 2.0 | 6 votes |
@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 File: BlueprintService.java From cloudbreak with Apache License 2.0 | 6 votes |
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 File: SQLiteDialect.java From yeti with MIT License | 6 votes |
@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 File: HibernateSessionFlushingTests.java From spring-analysis-note with MIT License | 5 votes |
@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 File: HibernateSessionFlushingTests.java From java-technology-stack with MIT License | 5 votes |
@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 File: SessionStoreIntegrationTests.java From judgels with GNU General Public License v2.0 | 5 votes |
@Test void token_has_unique_constraint() { store.createSession("token123", "userJid1"); assertThatExceptionOfType(ConstraintViolationException.class) .isThrownBy(() -> store.createSession("token123", "userJid2")); }
Example #18
Source File: PersistenceExceptionMapper.java From devicehive-java-server with Apache License 2.0 | 5 votes |
@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 File: SpringHibernateTransaction.java From elide-spring-boot with Apache License 2.0 | 5 votes |
@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 File: RestExceptionHandler.java From spring-boot-exception-handling with MIT License | 5 votes |
/** * 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 File: RestExceptionHandler.java From spring-boot-exception-handling with MIT License | 5 votes |
/** * 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 File: HibernateConstraintViolationExceptionMapper.java From crnk-framework with Apache License 2.0 | 5 votes |
@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 File: HibernateConstraintViolationExceptionMapper.java From crnk-framework with Apache License 2.0 | 5 votes |
@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 File: SQLExceptionTypeDelegate.java From lams with GNU General Public License v2.0 | 5 votes |
@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 File: PersistenceExceptionConverter.java From keycloak with Apache License 2.0 | 5 votes |
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 File: BIObjectParameterDAOHibImpl.java From Knowage-Server with GNU Affero General Public License v3.0 | 5 votes |
/** * 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 File: RestExceptionHandler.java From spring-boot with Apache License 2.0 | 5 votes |
/** * 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 File: RestExceptionHandler.java From spring-boot with Apache License 2.0 | 5 votes |
/** * 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 File: RoleService.java From abixen-platform with GNU Lesser General Public License v2.1 | 5 votes |
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 File: HibernateExceptionUnitTest.java From tutorials with MIT License | 5 votes |
@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); } } }