Help

After many months of polishing, we are happy to release Hibernate Validator 4. This is a major milestone for Hibernate Validator with tons of new features and a spec compliance.

For the newcomers

Hibernate Validator let's you declare constraints on your domain model using annotations like @NotNull or @Size and returns the list of constraint failures found in an object graph. Instead of duplicating constraint declarations in various application layers, constraints are centralized on your domain model and shared by all layers and frameworks: declared once, validate anywhere if you will.

What's new and cool?

Hibernate Validator is a complete rewrite and has many many many new features. Let me describe a handful of them:

  • constraint composition: a custom constraint can be composed of smaller constraints, avoiding code duplication, improving readability and increasing portability especially when combined with the set of built-in constraints.
  • groups: groups allow you to define a subset of the constraints you want to see validated at a given time. Useful in many situations like partially filled data, check the state of an object (can this user buy in one click?), order constraint validations. Oh and groups are not mere strings, they are a type-safe construct build on top of Java interfaces.
  • type-safe constraint declaration: Hibernate Validator 4 ensures that constraints set on a property are compatible with the property's type. This can even be theoretically checked at compile time.
  • more powerful custom constraints: as easy as before, more powerful than before. You can now customize the constraint violation messages reported by custom constraints and potentially return several violations if needed. A violation can point to a sub-property (useful for cross-property validations).
  • native integration with JPA 2 and JSF 2: Hibernate Validator 4 natively integrates with Java Persistence 2 and Java Server Faces 2 due to it's Bean Validation compliance. This integration is in the work in JBoss AS 5.2. People using Seam and Hibernate Core are already familiar with these style of integration since Hibernate Validator 3.
  • fluent type-safe bootstrap API: you can refine many aspects of Hibernate Validator like the message interpolation logic using the new bootstrap API.
  • metadata API: frameworks in need to query the constraints on a domain model can use the metadata API. This is for example used by Hibernate Core to propagate constraints to the database schema.
  • XML: XML configuration can be used in combination with or instead of annotations allowing for example redefinition of some constraints depending on the deployment environment.
  • a much improved test suite: we have completely rewritten the test suite which is now the base of the specification TCK.
  • compliance with JSR-303 Bean Validation: last but not least, Hibernate Validator 4 is the reference implementation, we couldn't make it more compliant ;) Practically, for you, it means you code against the specification API making your code more portable and your constraints will be visible by the whole Java ecosystem provided that they integrate Bean Validation (like Java EE 6 did for example).

Of course there are many other new features, check out the documentation here. You can download Hibernate Validator 4 from there and reach us in our forum.

The second good news is that the Bean Validation specification is now finished. I will hand it over to the JCP for final approval ballot today. More on that in a few days.

Many thanks to Hardy, the person behind Hibernate Validator 4's implementation for coping with last month/day/minute/second enhancements to the specification. A special thanks to Gunnar Morling and Alaa Nassef for their contribution despite a few administrative hiccups :)

6 comments:
14. Oct 2009, 06:49 CET | Link
Frank
There's an example .hbm in hibernate distribution. Does any one know how to write it in hibernate annotation? Thanks.

Master.hbm.xml

Code:
<hibernate-mapping package="org.hibernate.test.formulajoin">
   <class name="Master" table="t_masters">
      <id name="id">
         <generator class="increment"/>
      </id>
      ...
      <many-to-one name="detail"
            property-ref="currentMaster"
            insert="false"
            update="false">
         <column name="id"/>
         <formula>1</formula>
      </many-to-one>
   </class>
...
 
15. Oct 2009, 19:27 CET | Link
Stephan Bublava | sbublava(AT)me.com

Very nice! Is there an overview on the spec changes since 1.0.CR5? That might be quite helpful for upgrading my constraints.

Cheers, Stephan

 
16. Oct 2009, 01:41 CET | Link
Stephan Bublava
Stephan Bublava wrote on Oct 15, 2009 13:27:
Very nice! Is there an overview on the spec changes since 1.0.CR5? That might be quite helpful for upgrading my constraints.

It seems that just a few methods have been renamed. Upgrading took just a few minutes ...

Cheers, Stephan

21. Oct 2009, 07:16 CET | Link

I'd like to point out that Metawidget is another example of a framework that uses the Bean Validation metadata API. Metawidget reads the validation constraints to set attributes of an input field such as whether it's required, the width, max length and precision. See the reference for the BeanValidationInspector for a summary. If you think of another way to use this information, feel free to let Richard know.

 
13. Dec 2014, 11:41 CET | Link

This is cool post and i enjoy to read this post. your blog is fantastic and you have good staff in your blog. nice sharing keep it up.

freedebtconsolidationquotes.com
 
17. Dec 2014, 21:46 CET | Link

Many thanks for this brilliant post! Many points have extremely useful. Hopefully you'll continue sharing your knowledge around.

the official website