-->
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.  [ 11 posts ] 
Author Message
 Post subject: Hibernate Validation (JSR 303) not catching violations
PostPosted: Mon Jun 28, 2010 10:19 am 
Newbie

Joined: Mon Oct 26, 2009 3:38 pm
Posts: 18
I'm using Hibernate Validator to validate form submissions; currently, however, validation is not taking place. I've annotated certain properties with @NotEmpty, and even though I submit an empty form (all blank) the validator returns 0 violations. Any one know what is causing this malfunction? Below is my code:

Spring DI --
Code:
<bean id="validatorFactory" class="javax.validation.Validation" factory-method="buildDefaultValidatorFactory"/>   
<bean id="validator" factory-bean="validatorFactory" factory-method="getValidator"/>


Data Transfer Object with validation annotations --
Code:
import org.hibernate.validator.constraints.NotEmpty;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="registration")
public class RegistrationDto {
    private String firstName;
    private String lastName;
    ...
    @NotEmpty(message="Must provide first name.")
    public String getFirstName() {
        return firstName;
    }
    ...
    @NotEmpty(message="Must provide last name.")
    public String getLastName() {
        return lastName;
    }
    ...


Controller --
Code:
        // Spring injects validator with instance of validator class.
        Set<ConstraintViolation<RegistrationDto>> constraintViolations = validator.validate(registration)
       // constraintViolations (size) is ALWAYS zero


Top
 Profile  
 
 Post subject: Re: Hibernate Validation (JSR 303) not catching violations
PostPosted: Mon Jun 28, 2010 10:57 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Code:
Set<ConstraintViolation<RegistrationDto>> constraintViolations = validator.validate(registration)


Registration is an instance of RegistrationDto? Is there anything in the log file? Turn on debug level log for org.hibernate.validator and check if this gives any hints on what's going on.

--Hardy


Top
 Profile  
 
 Post subject: Re: Hibernate Validation (JSR 303) not catching violations
PostPosted: Mon Jun 28, 2010 4:38 pm 
Newbie

Joined: Mon Oct 26, 2009 3:38 pm
Posts: 18
Yes, registration is an instance of RegistrationDto.

I turned on debug level logging and the following is what was returned:
Quote:
util.Version Hibernate Validator 4.0.2.GA
engine.ResourceBundleMessageInterpolator ValidationMessages not found. Delegating to org.hibernate.validator.ValidationMessages
resolver.DefaultTraversableResolver Found javax.persistence.PersistenceUtil on classpath.
resolver.DefaultTraversableResolver Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver.
xml.ValidationXmlParser No META-INF/validation.xml found. Using annotation based configuration only
Do you see what's wrong?


Top
 Profile  
 
 Post subject: Re: Hibernate Validation (JSR 303) not catching violations
PostPosted: Tue Jun 29, 2010 4:22 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
The log shows that the validator framework properly bootstraps.
Are you sure that the fields you are validating don't have any whitespaces or something? @NotEmpty is not trimming the string.


Top
 Profile  
 
 Post subject: Re: Hibernate Validation (JSR 303) not catching violations
PostPosted: Tue Jun 29, 2010 10:23 am 
Newbie

Joined: Mon Oct 26, 2009 3:38 pm
Posts: 18
My app is grails based with chunks of pure java ( I don't know if that makes a difference, but I thought it best to share that fact).

In any event, I reloaded my (ivy2) dependency cache, and in doing so I got the following error message:
Quote:
java.lang.NoSuchMethodError: javax.persistence.Persistence.getPersistenceUtil()Ljavax/persistence/PersistenceUtil;
I'm confused as to why I'm getting this error. I checked my dependency report, and hibernate-validator pulls in jpa-api by org.hibernate.java-persistence (Revision: 2.0.Beta-20090815) As I understand it, the problem is when you have a missing or incomplete JPA 2.0 in the classpath.

Also it seems the way to resolve this is to "bootstrap Bean Validation with a custom TraversableResolver... use DefaultTraversableResolver just without the JPA checking code." (viewtopic.php?p=2422807)

Is my understanding of the problem correct? Are there any alternative resolutions? How would you recommend I proceed? Thanks.


Top
 Profile  
 
 Post subject: Re: Hibernate Validation (JSR 303) not catching violations
PostPosted: Tue Jun 29, 2010 10:44 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hibernate Validator itself does not depend on JPA 2. It is an optional dependency. Validator will check at runtime whether the JPA dependencies are on the classpath. If so it will delegate to a JPATraversableResolver.
Which version of Validator are you using, btw? The current version of the JPA dependency is org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.0.Final. That's why I'm asking. I recommend you upgrade to Validator 4.1.0.Final.
And yes, you can force a specific TraversableResolver during bootstrapping.


Top
 Profile  
 
 Post subject: Re: Hibernate Validation (JSR 303) not catching violations
PostPosted: Tue Jun 29, 2010 1:03 pm 
Newbie

Joined: Mon Oct 26, 2009 3:38 pm
Posts: 18
I was using 4.0.2.GA (that was the latest version in the jboss maven2 repo). As you suggested I upgraded to 4.1.0.Final. Unfortunately, however, I continue to get:
Quote:
java.lang.NoSuchMethodError: javax.persistence.Persistence.getPersistenceUtil()Ljavax/persistence/PersistenceUtil;
I even added org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.0.Final to my dependency list, but that didn't solve the error either.

Any other suggestions on what could be causing the error & how to resolve it? Does the getPersistenceUtil() error mean I have to implement a custom TraversableResolver?


Top
 Profile  
 
 Post subject: Re: Hibernate Validation (JSR 303) not catching violations
PostPosted: Wed Jun 30, 2010 4:45 am 
Hibernate Team
Hibernate Team

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

Interesting, which jpa api version does get included now? org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.0.Final?
To solve your problem regarding the traversable resolvers - you have several options:

  • JPA is an optional dependency for Validator. If you are not using JPA find out where and why the JPA dependency gets included and remove/exclude it.
  • As you mentioned earlier, the error indicates a incomplete JPA 2 implementation. Hibernate Validator checks for javax.persistence.PersistenceUtil on the classpath. If this class is on the classpath it assumes JPA 2 is available and instantiates a JPATraversableResolver. This resolver in turn will call Persistence.getPersistenceUtil().isLoaded(). If javax.persistence.PersistenceUtil is on the classpath, but Persistence.getPersistenceUtil() does not exist you are having a old/incomplete version of the JPA 2 on the classpath. Make sure you are using the right JPA 2 version if you want to use JPA.
  • You can implement a custom TraversableResolver and avoid the problem altogether, but somehow you are covering up some dependency resolving issues which seem to exist in your setup. Implementing a TraversableResolver is btw very easy. Just return true in both of the methods you have to implement.

--Hardy


Top
 Profile  
 
 Post subject: Re: Hibernate Validation (JSR 303) not catching violations
PostPosted: Wed Jun 30, 2010 6:14 pm 
Newbie

Joined: Mon Oct 26, 2009 3:38 pm
Posts: 18
The current jpa version that is included is: org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.0.Final

I ensured that persistence-api was NOT in the classpath, but the error (see below) continued

Quote:
java.lang.NoSuchMethodError: javax.persistence.Persistence.getPersistenceUtil()Ljavax/persistence/PersistenceUtil;


I was able to solve the problem by excluding hibernate-jpa from the build. However, I'm confused about the following:

  • Why did the error continue in spite of there being (only) hibernate-jpa in the classpath?
  • What is the difference between JPATraversableResolver and any other TraversableResolver?

Thanks

-Dan


Top
 Profile  
 
 Post subject: Re: Hibernate Validation (JSR 303) not catching violations
PostPosted: Thu Jul 01, 2010 3:39 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Quote:
I ensured that persistence-api was NOT in the classpath, but the error (see below) continued
Quote:
java.lang.NoSuchMethodError: javax.persistence.Persistence.getPersistenceUtil()Ljavax/persistence/PersistenceUtil;



I can this only happen if you have only the jpa api on the classpath, but no implementation. But maybe this is exactly what you have, since you don't seem to need JPA.

Quote:
I was able to solve the problem by excluding hibernate-jpa from the build. However, I'm confused about the following:

  • Why did the error continue in spite of there being (only) hibernate-jpa in the classpath?
  • What is the difference between JPATraversableResolver and any other TraversableResolver?


For the first question see above. Regarding the second, TraversableResolver is not a concrete class, but an interface. There is however something like a default implementation if the interface which will for both methods of the interface (isReachable and isCascadable) will just always return true. The idea is that in the case of JPA and lazy loaded association you want to avoid loading lazy data from the database in order to validate it. If it is already persistent in the database it should already have been validated and there is no need to retrieve and validate it again. Have a look at the Bean Validation specification if you want to know more.

--Hardy


Top
 Profile  
 
 Post subject: Re: Hibernate Validation (JSR 303) not catching violations
PostPosted: Thu Jul 01, 2010 9:53 pm 
Newbie

Joined: Mon Oct 26, 2009 3:38 pm
Posts: 18
hardy.ferentschik wrote:
I can this only happen if you have only the jpa api on the classpath, but no implementation. But maybe this is exactly what you have, since you don't seem to need JPA.
Aha! I mis-interpreted the docs and thought that ONLY the jpa api was needed.
hardy.ferentschik wrote:
For the first question see above. Regarding the second, TraversableResolver is not a concrete class, but an interface. There is however something like a default implementation if the interface which will for both methods of the interface (isReachable and isCascadable) will just always return true. The idea is that in the case of JPA and lazy loaded association you want to avoid loading lazy data from the database in order to validate it. If it is already persistent in the database it should already have been validated and there is no need to retrieve and validate it again. Have a look at the Bean Validation specification if you want to know more.

--Hardy
Thanks for the explanation; it cleared up my understanding.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 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.