del.icio.us Digg DZone Reddit StumbleUpon
Tutorial: Getting Started with Hibernate Validator - Willie Wheeler
« Previous | 1 | 2 | 3 | 4 | 5 | Next »

How to tell Hibernate Validator to validate annotated beans

It's fairly straightforward to get Hibernate Validator to validate our annotated beans. Listing 3 presents some demo code for doing exactly this. Let's take a look.

Listing 3: Demo.java, which shows how to use Hibernate Validator to validate annotated beans
package com.wheelersoftware.demos.hibernatevalidator;

import org.hibernate.validator.ClassValidator;
import org.hibernate.validator.InvalidValue;

public final class Demo {
    private static ClassValidator<User>
        userValidator = new ClassValidator<User>(User.class);

    public static void main(String[] args) {
        validateUser(createUser());
    }

    private static User createUser() {
        User user = new User();
        user.setFirstName("123456789012345678901");
        user.setEmail("aol.com");

        Address addr = new Address();
        addr.setStreet1("");
        addr.setCity("Moreno Valley");
        addr.setState("CA");
        addr.setZip("QWERTY");
        user.setAddress(addr);

        return user;
    }

    private static void validateUser(User user) {
        InvalidValue[] invalidValues = userValidator.getInvalidValues(user);
        for (InvalidValue value : invalidValues) {
            System.out.println("========");
            System.out.println(value);
            System.out.println("message=" + value.getMessage());
            System.out.println("propertyName=" + value.getPropertyName());
            System.out.println("propertyPath=" + value.getPropertyPath());
            System.out.println("value=" + value.getValue());
        }
    }
}

There are really only a few interesting things happening here. First, we create a ClassValidator<User> instance for validating our User beans. We have to associate the ClassValidator with a type (here, User) because this is where Hibernate Validator goes in and reads all the annotations off of the bean class in question.

Next, we have to create the bean we want to validate. Usually this would come from a user form (for example, a web-based form, or maybe a Swing-based form) though that's not necessarily the case. Here we just create a bean manually, and we intentionally make a lot of the fields invalid so we can see how Hibernate Validator responds.

Finally we make the call userValidator.getInvalidValues(user). This generates an array of InvalidValue instances—one for each validation constraint violation. Let's examine the InvalidValue class in more detail.

Social bookmarks: del.icio.us Digg DZone Reddit StumbleUpon
« Previous | 1 | 2 | 3 | 4 | 5 | Next »

Comments (22)

Also, Hibernate Validator 4.0.0 Alpha just came out, and I believe it's a reference implementation for JSR 303. Here are some relevant links:

By Willie Wheeler on Feb 17, 2009 at 1:46 AM PST

Oh, the spec itself would be good too. Here's the latest working version.

By Willie Wheeler on Feb 17, 2009 at 1:48 AM PST

Thanks for that interesting introduction!

Personally, i'm kind of unhappy with JSR 303, since it doesn't provide the full power 'Design By Contract' (in form of preconditions for the case of formulating 'input' constraints, not talking about invariants and postcondtions) could give you.

I wonder why to be restricted to a limited set of constraints instead of being able to express custom conditions using an adequate constraint (or predicate) language.

In addition to that, i wonder why you explicitly have to apply a 'Helper' in order to get into action and trigger validation, which is pretty decoupled from the causer of the constraint violation (instead of obeying the stated contract 'permanently', whenever you try to call an annotated method, clearly pointing to the causer of the constraint violation).

For an example of DBC you might want to take a look at

http://gleichmann.wordpress.com/2007/11/21/springcontracts-design-by-contract-with-seamless-integration-into-spring-is-now-open-source/

for a first starting point (while there are of course some other solutions for DBC).

Greetings

Mario

By Mario Gleichmann on Feb 18, 2009 at 1:06 PM PST

I'm interested in how to "render Hibernate Validator error messages out using Spring Web MVC taglibs". Is there a better way than simply converting the Hibernate InvalidValue object to a BindingResult object?

By Chris on Feb 18, 2009 at 3:14 PM PST

@Mario: Thanks for the thoughts and link. I will check your blog post out.

@Chris: The approach I've taken so far is exactly the one you just described. Whether there's a better approach or not I don't know, but at some level it would seem that the conversion has to take place, even if Spring 3 ends up handling the conversion at the framework level (which is what I would expect).

By Willie Wheeler on Feb 19, 2009 at 5:53 PM PST

We're not going to worry about integrating Hibernate Validator with Spring's native validation framework (so that, for instance, we might render Hibernate Validator error messages out using Spring Web MVC taglibs) though I'll probably write another article on that sometime in the future if people are interested

I'm interested :)

By JT on Apr 6, 2009 at 3:30 PM PDT

Thanks for the tutorial. That's really very explanatory.

I will have a question about the ClassValidator declaration. Let's assume User class extends the abstract class Person and getting annotations through Person class is needed.

ClassValidator personValidator = new ClassValidator (Person.class);

InvalidValue[] invalid Values = itemValidator.get Invalid Values(person);

I couldn't get it working. No invalid values are found. Is there a way to get invalid values in a child class of an abstract class through annotations?

The Person class might seem weird but I am just trying to illustrate the situation in my project. There exists an abstract class and there are many classes extending it. And there are custom validator annotations are declared in those classes and i want to validate these classes through one ClassValidator declaration but i couldn't succeed.

Any help would be appreciated!

By Ilker Ozen on May 11, 2009 at 12:24 AM PDT

I keep coming back to this article. Very useful and well-written - thank you!

By Frederic Daoud on Aug 2, 2009 at 4:25 AM PDT

many Thks

By MJA on Aug 5, 2009 at 9:28 AM PDT

Great article, and your Manning book is coming along nicely!

By Gordon Dickens on Sep 9, 2009 at 10:42 AM PDT

Nice article , It helps to the begginers ...... thanks

By Sandeep Natoo on Sep 14, 2009 at 4:32 AM PDT

The Hibernate 4.0, a reference implementation of the Bean validator, seems to be a different animal. The APIs are not the same, nor the usage.

By vicina on Oct 4, 2009 at 11:53 PM PDT

@vicina: Yup, Hibernate 4 is the JSR 303 reference implementation now, and Hibernate 3.1 is now legacy.

By Willie Wheeler on Oct 24, 2009 at 3:32 PM PDT

(Oops, in the message above I meant "Hibernate Validator 4" and "Hibernate Validator 3.1.")

Regarding integrating Hibernate Validator with Spring, Spring 3 now supports this directly. See the Spring 3 reference docs.

By Willie Wheeler on Oct 24, 2009 at 9:55 PM PDT

Hi,

Its a very good article to start with hibernate validator framework..........

By Shivendra on Dec 7, 2009 at 8:43 PM PST

Thanks a lot for your outstanding article.

By Fernando on Jan 20, 2010 at 2:54 PM PST

Excellent article.

By Rajesh Koilpillai on Jan 25, 2010 at 3:00 AM PST

hi, its working fine but if i configure it with class that extends "ListResourceBundle"

then i am not getting desired output.. is there any thing else i need to specify ?

By xyz on Feb 9, 2010 at 2:58 AM PST

It is really a Nice article and helps to the begginers, many thanks!

By William on May 26, 2010 at 11:02 AM PDT

It would be better if there is a link to print it nicly. Thanks a lot.

By William on May 26, 2010 at 11:03 AM PDT

I still use Spring 2.5.6 (don't plan to upgrade to Spring 3.0 yet). Which Hibernate Validator should I use (3.1.0)? I'm interested in how to "render Hibernate Validator error messages out using Spring Web MVC taglibs" (same question as Mario). Is there a better way than simply converting the Hibernate InvalidValue object to a BindingResult object? Any tutorial or sample code available?

By albert kao on Jun 14, 2010 at 8:08 AM PDT

like!

By javadood on Jun 15, 2010 at 1:52 PM PDT

Post a comment

Your name:
Your e-mail address (won't be displayed):
Your web site (optional):
example: www.xyz.com
Your comment:
Preview:
By You
Please help us reduce comment spam:
Spring in Practice
My brother and I are writing Spring in Practice for Manning!

What's New?

2009-08-30 - Check out my two-part series on DZone: Spring Integration: A Hands-On Tutorial.
2009-03-25 - My new article Getting Started with Spring Batch 2.0 is available on DZone.
Home | Consulting | Tech Articles | Mailing List | Contact | Spring Blog
Copyright © 2008 Wheeler Software, LLC.