Search in sources :

Example 6 with ConstraintViolationException

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);
        }
    }
}
Also used : TraversableResolver(javax.validation.TraversableResolver) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) Validator(javax.validation.Validator) HashSet(java.util.HashSet)

Example 7 with ConstraintViolationException

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();
}
Also used : EntityManager(javax.persistence.EntityManager) ConstraintViolationException(javax.validation.ConstraintViolationException) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 8 with ConstraintViolationException

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();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolationException(javax.validation.ConstraintViolationException) BigDecimal(java.math.BigDecimal) Session(org.hibernate.Session) Test(org.junit.Test)

Example 9 with ConstraintViolationException

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();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 10 with ConstraintViolationException

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();
}
Also used : Transaction(org.hibernate.Transaction) ConstraintViolation(javax.validation.ConstraintViolation) ConstraintViolationException(javax.validation.ConstraintViolationException) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

ConstraintViolationException (javax.validation.ConstraintViolationException)37 Test (org.junit.Test)26 ConstraintViolation (javax.validation.ConstraintViolation)12 Session (org.hibernate.Session)11 Transaction (org.hibernate.Transaction)11 LogicalPlan (com.datatorrent.stram.plan.logical.LogicalPlan)9 BigDecimal (java.math.BigDecimal)7 HashSet (java.util.HashSet)5 Method (java.lang.reflect.Method)4 Set (java.util.Set)4 ValidationException (javax.validation.ValidationException)4 Validator (javax.validation.Validator)4 RpcException (com.alibaba.dubbo.rpc.RpcException)3 RecoverableInputOperator (com.datatorrent.stram.engine.RecoverableInputOperator)3 Date (java.util.Date)3 ValidationParameter (com.alibaba.dubbo.examples.validation.api.ValidationParameter)2 ValidationService (com.alibaba.dubbo.examples.validation.api.ValidationService)2 LocalMode (com.datatorrent.api.LocalMode)2 GenericOperator (com.datatorrent.stram.engine.GenericNodeTest.GenericOperator)2 List (java.util.List)2