use of javax.validation.ConstraintViolationException in project hibernate-orm by hibernate.
the class BeanValidationEventListener method validate.
private <T> void validate(T object, EntityMode mode, EntityPersister persister, SessionFactoryImplementor sessionFactory, GroupsPerOperation.Operation operation) {
if (object == null || mode != EntityMode.POJO) {
return;
}
TraversableResolver tr = new HibernateTraversableResolver(persister, associationsPerEntityPersister, sessionFactory);
Validator validator = factory.usingContext().traversableResolver(tr).getValidator();
final Class<?>[] groups = groupsPerOperation.get(operation);
if (groups.length > 0) {
final Set<ConstraintViolation<T>> constraintViolations = validator.validate(object, groups);
if (constraintViolations.size() > 0) {
Set<ConstraintViolation<?>> propagatedViolations = new HashSet<ConstraintViolation<?>>(constraintViolations.size());
Set<String> classNames = new HashSet<String>();
for (ConstraintViolation<?> violation : constraintViolations) {
LOG.trace(violation);
propagatedViolations.add(violation);
classNames.add(violation.getLeafBean().getClass().getName());
}
StringBuilder builder = new StringBuilder();
builder.append("Validation failed for classes ");
builder.append(classNames);
builder.append(" during ");
builder.append(operation.getName());
builder.append(" time for groups ");
builder.append(toString(groups));
builder.append("\nList of constraint violations:[\n");
for (ConstraintViolation<?> violation : constraintViolations) {
builder.append("\t").append(violation.toString()).append("\n");
}
builder.append("]");
throw new ConstraintViolationException(builder.toString(), propagatedViolations);
}
}
}
use of javax.validation.ConstraintViolationException in project hibernate-orm by hibernate.
the class BeanValidationTest method testBeanValidationIntegrationOnFlush.
@Test
public void testBeanValidationIntegrationOnFlush() {
CupHolder ch = new CupHolder();
ch.setRadius(new BigDecimal("12"));
ch.setTitle("foo");
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
try {
em.persist(ch);
em.flush();
fail("invalid object should not be persisted");
} catch (ConstraintViolationException e) {
assertEquals(1, e.getConstraintViolations().size());
}
assertTrue("A constraint violation exception should mark the transaction for rollback", em.getTransaction().getRollbackOnly());
em.getTransaction().rollback();
em.close();
}
use of javax.validation.ConstraintViolationException in project hibernate-orm by hibernate.
the class BeanValidationProvidedFactoryTest method testListeners.
@Test
public void testListeners() {
CupHolder ch = new CupHolder();
ch.setRadius(new BigDecimal("12"));
Session s = openSession();
Transaction tx = s.beginTransaction();
try {
s.persist(ch);
s.flush();
fail("invalid object should not be persisted");
} catch (ConstraintViolationException e) {
assertEquals(1, e.getConstraintViolations().size());
assertEquals("Oops", e.getConstraintViolations().iterator().next().getMessage());
}
tx.rollback();
s.close();
}
use of javax.validation.ConstraintViolationException in project hibernate-orm by hibernate.
the class HibernateTraversableResolverTest method testEmbedded.
@Test
public void testEmbedded() {
Session s = openSession();
Transaction tx = s.beginTransaction();
Screen screen = new Screen();
PowerSupply ps = new PowerSupply();
screen.setPowerSupply(ps);
Button button = new Button();
button.setName(null);
button.setSize(3);
screen.setStopButton(button);
try {
s.persist(screen);
s.flush();
fail("@NotNull on embedded property is not evaluated");
} catch (ConstraintViolationException e) {
assertEquals(1, e.getConstraintViolations().size());
ConstraintViolation<?> cv = e.getConstraintViolations().iterator().next();
assertEquals(Screen.class, cv.getRootBeanClass());
// toString works since hibernate validator's Path implementation works accordingly. Should do a Path comparison though
assertEquals("stopButton.name", cv.getPropertyPath().toString());
}
tx.rollback();
s.close();
}
use of javax.validation.ConstraintViolationException in project hibernate-orm by hibernate.
the class HibernateTraversableResolverTest method testCollectionAssocNotValidated.
@Test
public void testCollectionAssocNotValidated() {
Session s = openSession();
Transaction tx = s.beginTransaction();
Screen screen = new Screen();
screen.setStopButton(new Button());
screen.getStopButton().setName("STOOOOOP");
PowerSupply ps = new PowerSupply();
screen.setPowerSupply(ps);
Color c = new Color();
c.setName("Blue");
s.persist(c);
c.setName(null);
screen.getDisplayColors().add(c);
try {
s.persist(screen);
s.flush();
fail("Associated objects should not be validated");
} catch (ConstraintViolationException e) {
assertEquals(1, e.getConstraintViolations().size());
final ConstraintViolation constraintViolation = e.getConstraintViolations().iterator().next();
assertEquals(Color.class, constraintViolation.getRootBeanClass());
}
tx.rollback();
s.close();
}
Aggregations