Search in sources :

Example 16 with ConstraintViolationException

use of org.hibernate.exception.ConstraintViolationException in project irida by phac-nml.

the class UserServiceImplTest method testCreateUserWithNoConstraintViolationName.

@Test(expected = EntityExistsException.class)
public void testCreateUserWithNoConstraintViolationName() {
    User u = new User();
    ConstraintViolationException constraintViolationException = new ConstraintViolationException(null, null, null);
    DataIntegrityViolationException integrityViolationException = new DataIntegrityViolationException("Duplicate", constraintViolationException);
    when(userRepository.save(any(User.class))).thenThrow(integrityViolationException);
    when(validator.validateValue(eq(User.class), eq("password"), any(String.class))).thenReturn(new HashSet<ConstraintViolation<User>>());
    userService.create(u);
}
Also used : User(ca.corefacility.bioinformatics.irida.model.user.User) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Test(org.junit.Test)

Example 17 with ConstraintViolationException

use of org.hibernate.exception.ConstraintViolationException in project irida by phac-nml.

the class UserServiceImplTest method testCreateUserWithIntegrityConstraintViolations.

@Test(expected = EntityExistsException.class)
public void testCreateUserWithIntegrityConstraintViolations() {
    User u = new User();
    ConstraintViolationException constraintViolationException = new ConstraintViolationException("Duplicate", null, User.USER_USERNAME_CONSTRAINT_NAME);
    DataIntegrityViolationException integrityViolationException = new DataIntegrityViolationException("Duplicate", constraintViolationException);
    when(userRepository.save(any(User.class))).thenThrow(integrityViolationException);
    when(validator.validateValue(eq(User.class), eq("password"), any(String.class))).thenReturn(new HashSet<ConstraintViolation<User>>());
    userService.create(u);
}
Also used : User(ca.corefacility.bioinformatics.irida.model.user.User) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Test(org.junit.Test)

Example 18 with ConstraintViolationException

use of org.hibernate.exception.ConstraintViolationException in project vertigo by KleeGroup.

the class JpaDataStorePlugin method handlePersistenceException.

/**
 * Gestion centralisée des exceptions SQL.
 * @param pse Exception SQL
 */
private void handlePersistenceException(final PersistenceException pse) {
    Throwable t = pse.getCause();
    // On ne traite que les violations de contraintes
    if (!(t instanceof ConstraintViolationException)) {
        throw pse;
    }
    final ConstraintViolationException cve = (ConstraintViolationException) t;
    // On récupère l'erreur SQL associé
    t = cve.getCause();
    if (!(t instanceof SQLException)) {
        throw pse;
    }
    final SQLException sqle = (SQLException) t;
    throw sqlDataBase.getSqlExceptionHandler().handleSQLException(sqle, null);
}
Also used : SQLException(java.sql.SQLException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException)

Example 19 with ConstraintViolationException

use of org.hibernate.exception.ConstraintViolationException in project flytecnologia-api by jullierme.

the class FlyExceptionHandler method handleDataIntegrityViolationException.

@ExceptionHandler(DataIntegrityViolationException.class)
public ResponseEntity<Object> handleDataIntegrityViolationException(DataIntegrityViolationException ex, WebRequest request) {
    String fieldError = "resource.operation-not-allowed";
    if (ex.getCause() instanceof ConstraintViolationException) {
        fieldError = ((ConstraintViolationException) ex.getCause()).getConstraintName();
    }
    List<Error> errors = getListOfErros(fieldError, ex);
    return handleExceptionInternal(ex, errors, new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) FieldError(org.springframework.validation.FieldError) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler)

Example 20 with ConstraintViolationException

use of org.hibernate.exception.ConstraintViolationException in project vladmihalcea.wordpress.com by vladmihalcea.

the class HibernateOperationsOrderTest method test.

@Test
public void test() {
    final Long productId = transactionTemplate.execute(new TransactionCallback<Long>() {

        @Override
        public Long doInTransaction(TransactionStatus transactionStatus) {
            Company company = new Company();
            company.setName("TV Company");
            entityManager.persist(company);
            Product product = new Product("tvCode");
            product.setName("TV");
            product.setCompany(company);
            Image frontImage = new Image();
            frontImage.setName("front image");
            frontImage.setIndex(0);
            Image sideImage = new Image();
            sideImage.setName("side image");
            sideImage.setIndex(1);
            product.addImage(frontImage);
            product.addImage(sideImage);
            WarehouseProductInfo warehouseProductInfo = new WarehouseProductInfo();
            warehouseProductInfo.setQuantity(101);
            product.addWarehouse(warehouseProductInfo);
            entityManager.persist(product);
            return product.getId();
        }
    });
    try {
        transactionTemplate.execute(new TransactionCallback<Void>() {

            @Override
            public Void doInTransaction(TransactionStatus transactionStatus) {
                Product product = entityManager.find(Product.class, productId);
                assertEquals(2, product.getImages().size());
                Iterator<Image> imageIterator = product.getImages().iterator();
                Image frontImage = imageIterator.next();
                assertEquals("front image", frontImage.getName());
                assertEquals(0, frontImage.getIndex());
                Image sideImage = imageIterator.next();
                assertEquals("side image", sideImage.getName());
                assertEquals(1, sideImage.getIndex());
                Image backImage = new Image();
                backImage.setName("back image");
                backImage.setIndex(1);
                product.removeImage(sideImage);
                product.addImage(backImage);
                product.setName("tv set");
                entityManager.flush();
                return null;
            }
        });
        fail("Expected ConstraintViolationException");
    } catch (PersistenceException expected) {
        assertEquals(ConstraintViolationException.class, expected.getCause().getClass());
    }
    transactionTemplate.execute(new TransactionCallback<Void>() {

        @Override
        public Void doInTransaction(TransactionStatus transactionStatus) {
            Product product = entityManager.find(Product.class, productId);
            assertEquals(2, product.getImages().size());
            Iterator<Image> imageIterator = product.getImages().iterator();
            Image frontImage = imageIterator.next();
            assertEquals("front image", frontImage.getName());
            Image sideImage = imageIterator.next();
            assertEquals("side image", sideImage.getName());
            Image backImage = new Image();
            backImage.setName("back image");
            backImage.setIndex(1);
            // http://docs.jboss.org/hibernate/orm/4.2/javadocs/org/hibernate/event/internal/AbstractFlushingEventListener.html#performExecutions%28org.hibernate.event.spi.EventSource%29
            product.removeImage(sideImage);
            entityManager.flush();
            product.addImage(backImage);
            product.setName("tv set");
            entityManager.flush();
            return null;
        }
    });
}
Also used : WarehouseProductInfo(com.vladmihalcea.hibernate.model.store.WarehouseProductInfo) Company(com.vladmihalcea.hibernate.model.store.Company) TransactionStatus(org.springframework.transaction.TransactionStatus) Product(com.vladmihalcea.hibernate.model.store.Product) Image(com.vladmihalcea.hibernate.model.store.Image) Iterator(java.util.Iterator) PersistenceException(javax.persistence.PersistenceException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) Test(org.junit.Test)

Aggregations

ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)38 Test (org.junit.Test)16 PersistenceException (javax.persistence.PersistenceException)12 Session (org.hibernate.Session)11 SQLException (java.sql.SQLException)7 RObject (com.evolveum.midpoint.repo.sql.data.common.RObject)4 ObjectAlreadyExistsException (com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)4 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)4 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)4 HttpHeaders (org.springframework.http.HttpHeaders)4 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)4 User (ca.corefacility.bioinformatics.irida.model.user.User)3 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)3 Connection (java.sql.Connection)3 AbstractManyToManyAssociationClassTest (org.hibernate.test.manytomanyassociationclass.AbstractManyToManyAssociationClassTest)3 ResultModels (eu.bcvsolutions.idm.core.api.dto.ResultModels)2 DefaultErrorModel (eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel)2 ErrorModel (eu.bcvsolutions.idm.core.api.exception.ErrorModel)2 ErrorData (io.crnk.core.engine.document.ErrorData)2 PreparedStatement (java.sql.PreparedStatement)2