Cascade validation with the @Valid annotation does not work if the domain objects are populated with data from a database (when editing the data inside a method using the @ModelAttribute annotation in Spring 3.0). Validation errors such as "may not be empty" (@NotEmpty) occurs on submit for the cascaded validation, even though all the required fields are filled out.
This issue was first raised on the Spring forum and has the following JIRA issue as well:
https://jira.springsource.org/browse/SPR-7319According to Juergen Hoeller at Spring Community, the problem may be that Hibernate Validator fails to treat Hibernate-loaded lazy references correctly.
The problem may be reproduced by running an application I have created. It is available here:
http://folk.uio.no/erlendfg/validator/In order to run the application, just download and unzip, run "mvn jetty:run" and go to "http://localhost:8080/validator_test. A Derby database is included.
The problem occurs when this method runs:
Code:
@Transactional(readOnly = true)
@ModelAttribute("foo")
public Foo formBackingObject(@RequestParam(value = "id", required = false) Long id) {
if (id != null) {
Foo foo = fooDAO.findById(id);
return foo;
}
return new Foo();
}
But if I populate the domain object which fails to validate correctly manually (Bar), without a database, the problem disappears. Example:
Code:
bar.setObligatory("some content");
foo.setBar(bar);