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);
}
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);
}
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);
}
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);
}
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;
}
});
}
Aggregations