5

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.

user659580
  • 151
  • 2
  • 3
  • 10

2 Answers2

5

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.

user659580
  • 151
  • 2
  • 3
  • 10
  • 8
    but disabling a validation does not solve the core of the problem, it just ignore it. – Ralph May 24 '11 at 07:05
  • Thanks for it. But if you can investigate more on it that why validation was a problem. Do please share here. – Pujan Jun 22 '13 at 14:42
1

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
Ralph
  • 118,862
  • 56
  • 287
  • 383
  • Thanks for your answer. I'm indeed using Hibernate validator annotations at the class level to validate my classes when binding them from the form. But for this particular class, I did not specify any constraint, which makes me think that this is more like an internal thing. i'm not sure where to go from now. – user659580 May 23 '11 at 12:49
  • user659580: The Exception does not state that the constraint violation is related to class Person! – Ralph May 23 '11 at 13:59
  • but the exception happens when I try to flush or commit Person even right after the persist operation. – user659580 May 23 '11 at 14:08
  • @user659580 and nothing else is loades or changed? – Ralph May 23 '11 at 14:13
  • Are the any Bean Validation constraints in your object graph? Is there not more information in the log? There should be the error path logged somewhere. This would give you an idea which constraint on which object failed. – Hardy May 23 '11 at 14:26
  • @Ralph, yes I do get the error when I instantiate a new Person, assign couple of values and try to flush or commit after persisting. @Hardy Even though I use Hibernate Validator in my app, I do not have any constraints validation associated to these classes at this point. In fact the validation result returns no errors when I submit the form and bind it with the bean definition. – user659580 May 23 '11 at 14:33
  • @user659580: please try to deactivate the bean validation for saving (persistence.xml) and then try it again - to make sure that this is really the problem. – Ralph May 23 '11 at 14:39
  • @Ralph I removed Hibernate validator from the classpath but it is still not solving the issue.. It is getting really frustrating over here. Thx – user659580 May 23 '11 at 19:53
  • 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. – user659580 May 23 '11 at 20:47