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

21,131

Solution 1

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.

Solution 2

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.

Solution 3

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");
    }
Share:
21,131

Related videos on Youtube

Mark Estrada
Author by

Mark Estrada

Updated on July 09, 2022

Comments

  • Mark Estrada
    Mark Estrada almost 2 years

    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?

  • Mark Estrada
    Mark Estrada over 12 years
    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? Thanks
  • stratwine
    stratwine over 12 years
    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.