The latter is the reference implementation, which can be consumed as a standalone library, completely decoupled from the popular ORM framework.With that said, let’s see now how to get started using the Java Beans Validation / Hibernate Validator tandem for performing effective domain model validation in the real world.Should its state be validated before being persisted or updated in a database, or when passed around to other layer(s)? Prior to Java EE 6, Java didn’t provide a standard way of validating the fields of a domain class by mean of a centralized mechanism. The Java Beans Validation specification makes it fairly easy to selectively validate class fields (and even entire classes) by using constraints declared with a few intuitive annotations.
punck busted dont updating - Validating domain objects hibernate part 5
Finally, the Finally, validating a user object looks exactly the same as when using the default email validator. Of course, I’m not saying that you’ll always need to mess up your life by creating custom constraints and validators, as this would downgrade the functionality of Java Beans Validation to nearly zero.
Nevertheless, from a design point of view it is useful to know that the standard offers a decent level of customization.
At this point, it should be clear that Hibernate Validator is a hard-to-beat contender when it comes to validating domain objects in a straightforward fashion.
But there’s more yet: Even when JSR 303 ships by default with a robust set of constraint violations, which will cover the most typical validation requirements of daily developers like you and me, it’s worth mentioning that its core functionality can be easily extended by means of custom validation constraints and validators.
In fact, creating a custom validation constraint and a matching custom validator is a no-brainer process that boils down to following these steps: method allows to specify which constraints should be validated when the validator is called, in a per-group basis.
By default, all the constraints are checked within the default group, meaning that each constraint will be validated regardless of the target object’s lifecycle phase.
It’s possible, however, to get the same result by using a standard instances here and there, and validate the constrained classes in multiple places, even in the wrong ones!
But that would be a flagrant violation of the DRY Principle (aka a WET solution) that would lead to duplicated code across several layers. Instead, it’d be a lot more effective to encapsulate Hibernate Validator inside the boundaries of a decoupled service component, which could be potentially reused everywhere.
For instance, we could develop a basic web application, similar to the one I built in the Servlet API tutorial, and validate user data submitted through an HTML form in an effective and performant manner.
As building such web application is definitively out of this post’s scope, the task will be left as homework for you, in case that you want to start pulling the reins of Hibernate Validator in the Web terrain.
At this point, you’ve learned the basics of how to use Java Beans Validation and Hibernate Validator side by side, in order to validate your domain objects in a straightforward way.