-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Possible bug in Hibernate Validator: Cascading with @Valid
PostPosted: Tue Jun 29, 2010 4:20 am 
Newbie

Joined: Tue Jun 29, 2010 3:39 am
Posts: 3
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-7319
According 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);


Top
 Profile  
 
 Post subject: Re: Possible bug in Hibernate Validator: Cascading with @Valid
PostPosted: Tue Jun 29, 2010 8:03 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

unfortunately I cannot run your sample application, because several dependencies are not resolving when running 'mvn jetty:run'. First it it could not resolve the maven-framework-version-plugin plugin, so I removed it. After that com.springsource.org.hibernate.ejb caused problems. I think the version should be lower case 'ga'. Could it be that you run on Windows? But even after changing the version for com.springsource.org.hibernate.ejb I got a whole bunch of un-resolvable dependencies. Maybe you could update the sample project, maybe even create real unit tests.

Since I am not a Spring expert I don't know what @ModelAttribute actual does and where and how it fits in the whole request life cycle.

--Hardy


Top
 Profile  
 
 Post subject: Re: Possible bug in Hibernate Validator: Cascading with @Valid
PostPosted: Wed Jun 30, 2010 7:52 am 
Newbie

Joined: Tue Jun 29, 2010 3:39 am
Posts: 3
First of all, I'm deeply sorry that I didn't double-check the application on another environment before I posted this issue. I have uploaded a new version, and it should run on Linux and Mac OS X, even if it is deployed on Resin. I forgot to include a couple of repositories, and that's the reason of your build problems.

In case you experience some Derby problems, always run "clean" before you start the application:
mvn clean jetty:run

Press the "edit" link on the start page, and you will see that you're having validation errors for the Bar object even though all the required fields are filled out.

If a method has a @ModelAttribute annotation, it is always run before all other methods in the controller.


Top
 Profile  
 
 Post subject: Re: Possible bug in Hibernate Validator: Cascading with @Valid
PostPosted: Wed Jun 30, 2010 1:24 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
The problem is in your case the constraint placement. Validator will try to access the value to be validated using field access if the constraint is placed at field level and method access if it is defined on the method. Normally it should not matter. However, in your case you are using lazy loading which means that your Bar instance will be replaced by a proxy to allow lazy loading. When using proxies you always have to use method access, otherwise it will not work. Try placing the constraint on the methods and it should work. In fact consistent annotation placement makes sense anyway.

--Hardy


Top
 Profile  
 
 Post subject: Re: Possible bug in Hibernate Validator: Cascading with @Valid
PostPosted: Thu Jul 01, 2010 8:12 am 
Newbie

Joined: Tue Jun 29, 2010 3:39 am
Posts: 3
Hardy, you have no idea how thankful I am right now. We have struggled with this issue for weeks, and the solution came one day before my vacation. This means that I can have a summer vacation without thinking about the bugs I had to fix when I returned to my job.

And I learned something new about field and method access related to proxies.

Once again, thanks for helping us with this issue.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.