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 (32)

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

Our thesis writing is the best and the organization thank you a lot for your good enough knowledge just about this good post. Thus, this time some students have some knowledge just about dissertation and buy term paper.

By Piper18 on Feb 20, 2010 at 5:04 PM PST

Some persons do know that that is more simple to purchase the thesis examples and thesis writing service just about this post, than to make by own efforts.

By Mia30 on Feb 20, 2010 at 10:53 PM PST

To be successful means to get the high academic grade and for that, students should demonstrate the great quality essay order. But is it real to accomplish it not utilizing a support of the online writing service? Yes, it?s doable, but that is easier to buy do my essay assignment about this topic in web.

By HannahAa32 on Feb 21, 2010 at 12:44 AM PST

People can learn just about this post and I advice to select the essay writing and buy custom essay papers and that is available to purchase essay for sale.

By kh24Gemma on Feb 21, 2010 at 12:38 PM PST

I submitted the supreme topic just about this good post using article submission sites and some article submit service.

By QX21Gwen on Feb 21, 2010 at 1:40 PM PST

Good work! Your post/article is an excellent example of why I keep comming back to read your excellent quality content that is forever updated. Thank you!

By eskort bayan on Feb 23, 2010 at 12:51 AM PST

Very good your article, excellent quality content.Thank you...

By evden eve nakliyat on Feb 24, 2010 at 1:47 PM PST

You are making really very perfect contribution close to this topic! So, the custom dissertation services and the thesis writing service, should use for the thesis pdf.

By JOSEPHINE21Buchanan on Feb 27, 2010 at 12:48 AM PST

Some important fact referring to this good topic suppose to be provided by term paper writing service. And anyone would be able to buy essays or essay order.

By ESTELLEHarper19 on Mar 3, 2010 at 10:17 AM PST

admirable that you has eventually developed a self empowerment polity that is customarily serious and, yet, simple, joy, and available. They should take paper writing support

By LizaRamsey on Mar 5, 2010 at 2:22 AM PST

It's important for people to know by heart though, you need to buy term papers or buy research paper just because a school isn't the finest at everything doesn't mean it can't be the finest at some things. Essays blogs can bring more usefull for your function you can also buy essay. But first of all, my obligation to this article, it has a positive motivation.

By McleodFannie24 on Mar 5, 2010 at 6:15 AM PST

bgfbg

By bbvf on Mar 8, 2010 at 5:53 AM PST

I do not even know a proper way to show my feelings after the utilizing the help of custom writing service. I should tell that my novel essays are superb at present time. Paper online writers are professional.

By WhitfieldVIOLET18 on Mar 9, 2010 at 8:01 AM PST

You have rectified my anticipation; I am absolutely agreeable with your chapter it will help me in my essay papers or custom research paper writing. Thank you so much. I will doubtlessly keep in touch.

By SandersSasha29 on Mar 9, 2010 at 8:11 AM PST

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.