Bean Validation in Hibernate and JPA 2, Page 2
Bean Validation in Hibernate
The reference implementation of JSR 303 is Hibernate Validator. The validation.xml file is read when ValidationFactory is created, and Hibernate Validator applies the configurations. Hibernate Annotations will generate the various constraints applied to its persistent classes into metadata mapping. Setting the hibernate.validator.apply_to_ddl to false disables this mapping.
For example, if the
fname property of Employee is set with a constraint of @NotNull then it will be converted to its equivalent not null constraint for the corresponding column of the table in the database.
Hibernate Validator also has a built-in event listener, which is part of Hibernate Annotations, org.hibernate.cfg.beanvalidation.BeanValidationEventListener. The listener listens to events like PreInsertEvent, PreUpdateEvent and PreDeleteEvent, checks for all the constraints applied to the entities, and throws an exception when any constraints are violated.
In case of a validation failure, the information regarding the actual reason for the failure is thrown as a ConstraintViolationException that contains a collection of ConstraintViolation interface types. The reason can be determined by various methods of the ConstraintViolation interface such as
By default Hibernate Annotations uses Hibernate Validator for validation if it is set in the classpath, but this can be turned off by setting the javax.persistence.validation.mode property to none. Event listeners can be set manually in the hibernate.cfg.xml file as follows:
<hibernate-configuration> <session-factory> ... <property name="javax.persistence.validation.group.pre-persist">javax.validation.Default</property> <property name="javax.persistence.validation.group.pre-update">javax.validation.Default</property> <property name="javax.persistence.validation.group.pre-remove"></property> </session-factory> <event type="pre-update"> <listener class="org.hibernate.cfg.beanvalidation.BeanValidationEventListener"/> </event> <event type="pre-insert"> <listener class="org.hibernate.cfg.beanvalidation.BeanValidationEventListener"/> </event> <event type="pre-delete"> <listener class="org.hibernate.cfg.beanvalidation.BeanValidationEventListener"/> </event></hibernate-configuration>
Bean Validation in JPA 2
Bean validation is an optional feature in JPA 2, supported with its persistence provider. The persistence provider enables Bean validation on all entities to which annotations are applied. If Hibernate 3.5 is used as the persistence provider, then JPA 2 will use Hibernate Validator.
Event-based validation, which is performed in the hibernate.cfg.xml file in Hibernate, is configured in the persistence.xml file in JPA 2. Bean validation can be disabled by setting the validation-mode element in persistence.xml or the javax.persistence.validation.mode property to none.
In this article we drilled down into Bean validation in general and the reference implementation Hibernate Validator in particular, offering a brief comparison of how it is implemented in Hibernate and JPA 2.
The authors would like to sincerely thank Mr. Subrahmanya SV (VP, ECOM Research Group, E&R) for his ideas, guidance, support and constant encouragement and Ms. Mahalakshmi for kindly reviewing this article and providing valuable comments.
About the Authors
Sangeetha S. works as a Senior Technical Architect at the E-Commerce Research Labs at Infosys Technologies. She has over 10 years of experience in design and development of Java and Java EE applications. She has co-authored a book on 'J2EE Architecture' and also has written articles for online Java publications.
Nitin KL works at the E-Commerce Research Labs at Infosys Technologies. He is involved in design and development of Java EE applications using Hibernate, iBATIS, and JPA.