Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javax.validation.ConstraintViolationException: Bean Validation constraint(s) violated on preUpdate validation

I'm having an annoying error message while trying to insert new element in a many to many relationship using JPA 2.0, SpringMvc 3.0.

I have a table with States and another one with Persons. A person can be linked to many states and a state to many persons. In this particular case, I have a listOfStates and then a person and I would like to insert those elements in my many to many relationships.

ManyToMany Relationship (in table STATE)

    //bi-directional many-to-many association to Appointment
    @ManyToMany(cascade=CascadeType.ALL)
    @JoinTable(
name="PERSON_STATE"
, joinColumns={
    @JoinColumn(name="PERSON_ID", nullable=false)
    }
, inverseJoinColumns={
    @JoinColumn(name="CODE_STATE", nullable=false)
    }
)

DAO Code THAT I'm calling from my controller

try{    
    getEntityManager().getTransaction().begin();            
    getEntityManager().persist(myPerson);                       

    IStateDAO stateDAO = new StateDAO();

    for (int i=0; i<listOfStates.length; i++){
        State myState = stateDAO.findState(listOfStates[i]);
        if (myState != null){                   
            myState.getPersons().add(myPerson);
            getEntityManager().persist(myState);
        }
    }

    getEntityManager().getTransaction().commit();           
    getEntityManager().close();         

} catch (RuntimeException re) {
    getEntityManager().close();
    throw re;           
}

The funny thing is that this code is working fine when I'm not inserting data from a web page. What i am doing wrong here? I already have some persons and states in the DB.

Full Stack Error Message:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.validation.ConstraintViolationException: Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'preUpdate'. Please refer to embedded ConstraintViolations for details.


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

Any pointer would be really appreciated. Thanks in advance you all.

like image 892
user659580 Avatar asked May 23 '11 04:05

user659580


People also ask

What is javax validation ConstraintViolationException?

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

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.

How do you initialize ConstraintViolationException?

You can work around this like so: throw new ConstraintViolationException( new HashSet<ConstraintViolation<?>>(violations));

What does @NotNull annotation mean in bean property?

@NotNull is a JSR 303 Bean Validation annotation. It has nothing to do with database constraints itself. As Hibernate is the reference implementation of JSR 303, however, it intelligently picks up on these constraints and translates them into database constraints for you, so you get two for the price of one.


2 Answers

wow! got it! I had to change the validation-mode in my persistence.xml from Auto to NONE which basically tells the app not to used the bean validation at all. Error messages are gone and my DAO works well.

like image 60
user659580 Avatar answered Sep 29 '22 13:09

user659580


The Exceptions states that an JSR 303 Bean Validation is used, and Hibernate is configured (Persistence.xml) to check them before updating anything.

JSR 303 Bean Validation are annotations like:

  • javax.validation.constraints.NotNull
  • javax.validation.constraints.Size
like image 39
Ralph Avatar answered Sep 29 '22 11:09

Ralph