Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event: 'prePersist'
Asked Answered
E

4

13

I created an EJB Session facade in my Netbeans 7 for saving my entity. I have a manytoone mapping between my Insurance and RatePlan Class.

public class Insurance{
    @ManyToOne(optional=false) 
    @JoinColumn(name="PLAN_ID")
    private RatePlan plan;
}
public class RatePlan{
    @OneToMany(mappedBy="plan")
    private Set<Insurance> insuranceItems;
}

When I tried saving in my database using my EJB Session Bean, I am encountering below error.

Caused by: javax.validation.ConstraintViolationException: Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'prePersist'. Please refer to embedded ConstraintViolations for details.

What I did was to turn off my Bean validation in my Persistence.xml file. I would like to know what Bean validation error has occurred but I dont know how or where to find it or how to configure and catch it.

My EJB facade is a simple class like tis.

public class InsuranceFacade{
    public void saveInsurance(Insurance insurance){
        em.persist(insurance);
    }
}

Any hints?

Etruria answered 28/9, 2011 at 7:45 Comment(2)
I'm curious to know what violation it could be, when there are no bean validation specific annotations in these entities!Mehalek
I actually remove all of the bean validation annotation so that the code will be easier to read... =)Etruria
M
12

I would like to know what Bean validation error has occurred but I dont know how or where to find it or how to configure and catch it.

To know what specific constraint violations have occurred, you could just inspect the exception caught. ConstraintViolationException.getConstraintViolations() returns a Set of ConstraintViolations which you can iterate and inspect.

Mehalek answered 28/9, 2011 at 17:32 Comment(2)
Hi stratwine, do you mean in my EJB facade class and in the save operation, I would use a try catch block to check for this exception? Can you confirm my understanding? ThanksEtruria
This thread, with the inlined blog post shows how you could have ConstraintVioldationException as an ApplicationException, and hence get it "as it is" in the client. It looks good to me to have a catch block at the invoking client, checking for what was violated.Mehalek
S
7

I got the same problem, but after hours looking for the answer, Finally I Found it.... You should edit your AbstractFacade.java class and add this code

public void create(T entity) {

    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    Validator validator = factory.getValidator();
    Set<ConstraintViolation<T>> constraintViolations = validator.validate(entity);
    if(constraintViolations.size() > 0){
        Iterator<ConstraintViolation<T>> iterator = constraintViolations.iterator();
        while(iterator.hasNext()){
            ConstraintViolation<T> cv = iterator.next();
            System.err.println(cv.getRootBeanClass().getName()+"."+cv.getPropertyPath() + " " +cv.getMessage());

            JsfUtil.addErrorMessage(cv.getRootBeanClass().getSimpleName()+"."+cv.getPropertyPath() + " " +cv.getMessage());
        }
    }else{
        getEntityManager().persist(entity);
    }
}

Now this method will alert you which property and why it fails the validation. I hope this works for you, as it does for me.

Supplication answered 5/6, 2014 at 21:47 Comment(0)
G
6
catch (EJBException e) {
        @SuppressWarnings("ThrowableResultIgnored")
        Exception cause = e.getCausedByException();
        if (cause instanceof ConstraintViolationException) {
            @SuppressWarnings("ThrowableResultIgnored")
            ConstraintViolationException cve = (ConstraintViolationException) e.getCausedByException();
            for (Iterator<ConstraintViolation<?>> it = cve.getConstraintViolations().iterator(); it.hasNext();) {
                ConstraintViolation<? extends Object> v = it.next();
                System.err.println(v);
                System.err.println("==>>"+v.getMessage());
            }
        }
        Assert.fail("ejb exception");
    }
Goldiegoldilocks answered 29/4, 2014 at 7:32 Comment(0)
T
0

Catch the following exception where you persisting the entity. In my case its in the EJB add method. where I am doing em.persist(). Then check the server log, you will see which attribute having constrain violation.

catch (ConstraintViolationException e) {
       log.log(Level.SEVERE,"Exception: ");
       e.getConstraintViolations().forEach(err->log.log(Level.SEVERE,err.toString()));
    }
Turino answered 25/4, 2017 at 8:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.