Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 797
Mark Estrada Avatar asked Sep 28 '11 07:09

Mark Estrada


People also ask

What is Bean Validation constraints?

The Bean Validation model is supported by constraints in the form of annotations placed on a field, method, or class of a JavaBeans component, such as a managed bean. Constraints can be built in or user defined. User-defined constraints are called custom constraints.

What does @NotNull annotation mean in bean property?

The @NotNull annotation is defined in the Bean Validation specification. This means its usage isn't limited only to the entities. On the contrary, we can use @NotNull on any other bean as well. As we can see, in this case, our system threw javax.

How does Bean Validation work?

Very basically, Bean Validation works by defining constraints to the fields of a class by annotating them with certain annotations.

What is Bean Validation API?

The Bean Validation API provides a set of built-in constraints and an interface that enables you to declare custom constraints. This is accomplished by creating constraint annotations and declaring an annotation on a bean type, field, or property.


1 Answers

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.

like image 100
stratwine Avatar answered Nov 02 '22 01:11

stratwine