开发者

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

开发者 https://www.devze.com 2023-04-07 23:46 出处:网络
I created an EJB Session facade in my Netbeans 7 for saving my entity. I have a manytoone mapping between myInsurance and开发者_如何学编程 RatePlan Class.

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?


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.


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.


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


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()));
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号