Hibernate validation in a standalone implementation

Implement a consolidated validation strategy with Hibernate Validator

1 2 Page 2
Page 2 of 2

Validation rules can be expressed through the XML descriptor, overwriting any annotations defined in the code. In Listing 12 I disable all validation rules defined programmatically for the Address class. (Refer back to Listing 1 and make the optional fields defining PO box properties mandatory.)

Listing 12. Contents of an application-specific descriptor file

<constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://jboss.org/xml/ns/javax/validation/mapping" xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping ...">
<default-package>com.val.utils</default-package>
<bean class="Address" ignore-annotations="true">
	<field name="poBoxNumber">
		<constraint annotation="javax.validation.constraints.Pattern">
			<element name="regexp">[1-9]\\d*</element>
		</constraint>
	</field>
	<field name="station">
		<constraint annotation="org.hibernate.validator.constraints.NotBlank"/>
	</field>
</bean>
</constraint-mappings>

In conclusion

In this article I have highlighted the strength and flexibility of the Hibernate Validator framework through a set of simplified examples. See the source code with corresponding JUnit test cases for more detail about the examples. I hope your takeaway from this article is that it is possible to implement a powerful, standalone, and fully functional validation component with a relatively little effort.

It is possible to implement a generalized validation layer in an integration architecture in passive or active mode. In passive mode, the least invasive, validation results can be reported to logs, while the existing workflow and data exchange is not affected in any way. This design approach can be successfully used in the scenarios where existing architecture has to be extended with minimal impact to legacy code.

You can integrate a Hibernate Validation framework validation component with any Java SE, Java EE, and ESB framework to improve the reliability and maintainability of the overall application architecture. Hibernate Validator is well documented, and the 5.0.3 distribution contains an extensive set of examples, which can be useful for learning about the framework in depth and adapting it to a variety of applications.

This story, "Hibernate validation in a standalone implementation" was originally published by JavaWorld.

Copyright © 2014 IDG Communications, Inc.

1 2 Page 2
Page 2 of 2
InfoWorld Technology of the Year Awards 2023. Now open for entries!