Denis Defreyne
Photo credit: Denis Defreyne
del.icio.us Digg DZone Reddit StumbleUpon
1 | 2 | 3 | 4 | 5 | Next »
Software Development

Tutorial: Getting Started with Hibernate Validator

Use Hibernate Validator to define annotation-based bean validation constraints.

In this tutorial we're going to learn how to get started with Hibernate Validator, which as its name suggests is a validation framework associated with the Hibernate project. This article is really a follow-up to my earlier article on using the Bean Validation Framework (part of the larger Spring Modules project), which is a competing framework that seems to have lost the battle for Spring's "preferred validation provider" status to Hibernate Validator. Both Uri Boness (in an e-mail correspondence) and Juergen Hoeller (at SpringOne) agreed that people should start moving toward Hibernate Validator since that will eventually support the emerging JSR 303 standard.

Hibernate Validator is nice because it (like the Bean Validation Framework) supports declarative validation via Java 5 annotations. Let's say you create a bean class, like an Account or a PurchaseOrder or whatever. With Hibernate Validator you can attach validation annotations to the bean properties and that will define the validation constraints for the bean. Moreover, unlike earlier approaches to validation (such as Struts Validation), Hibernate Validator isn't tied to the web tier, and so if you want to validate your beans from within your service beans, or within your DAOs, or even just before you ORM them into your database, no sweat. You can do just that.

Anyway, for now we're just going to look at some of the basics: how to specify annotation constraints and how to check for constraint violations. 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.

For this article we're using Java 5 or higher (we need Java 5 annotations) and Hibernate Validator 3.1.0. For your convenience I've created a Maven 2 project that you can download.

OK, let's jump into some examples of annotated bean classes.

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

Comments (23)

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

Every point has got its way out. Thus, to buy essays from the research papers writing service can be a proper solution, I can tell you.

By BriannaHOOVER34 on Mar 14, 2010 at 7:41 AM PDT

Whatever path you resolve upon, there is always someone to tell you that you are failer. There are always difficulties come up which provoke you to think that your critics are veracious . But our service will support you at any time to write your topic. I offer you dissertation writing that will assist you in your university life. We will assist you to uprise and become a successful classman!

By Cynthia28Stark on Mar 14, 2010 at 7:50 AM PDT

Long time ago, I was writing the term papers by myself and very often I got not the highest marks. Hence I decided to attempt to get custom essays at essay papers service, then I acknowledged that it seemed to be the best decision of my life.

By GallagherDeana on Mar 14, 2010 at 9:11 AM PDT

Everything will be in order in your research process if you notice the experienced custom essay writing organization to purchase essays at. Or this used to be doable to get term paper.

By TYSON20Janna on Mar 16, 2010 at 2:53 AM PDT

Tiffany & Co Shop specialises in superior sterling silver Tiffany Jewelry, Tiffany Shop provides hundreds of discount and fashion Tiffany Jewellery, tiffany jewellery tiffany co tiffany GHD Styler Hair Straightener - shop online for ghd stylers, the new limited edition purple ghd IV styler & gift set, ghd hair care products, cheap ghd ghd straighteners ghd pink ghd ghd Hair Straighteners the women like ugg boots,ugg cardy boots,you could buy cheap ugg boots from here! ugg boots uggs

By Anonymous on Mar 16, 2010 at 6:49 AM 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 Annotations RefCard
Check out the new DZone Spring Annotations Refcard by Craig Walls!

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.