2.2. ORM integration
Hibernate Validator integrates with both Hibernate and all pure Java Persistence providers
2.2.1. Hibernate event-based validation
Hibernate Validator has two built-in Hibernate event listeners. Whenever a
PreInsertEvent
or PreUpdateEvent
occurs, the listeners will verify all constraints of the entity instance and throw an exception if any constraint is violated. Basically, objects will be checked before any inserts and before any updates made by Hibernate. This includes changes applied by cascade! This is the most convenient and the easiest way to activate the validation process. On constraint violation, the event will raise a runtime InvalidStateException
which contains an array of InvalidValue
s describing each failure.
If Hibernate Validator is present in the classpath, Hibernate Annotations (or Hibernate EntityManager) will use it transparently. If, for some reason, you want to disable this integration, set
hibernate.validator.autoregister_listeners
to false
Note
If the beans are not annotated with validation annotations, there is no runtime performance cost.
In case you need to manually set the event listeners for Hibernate Core, use the following configuration in
hibernate.cfg.xml
:
<hibernate-configuration> ... <event type="pre-update"> <listener class="org.hibernate.validator.event.ValidateEventListener"/> </event> <event type="pre-insert"> <listener class="org.hibernate.validator.event.ValidateEventListener"/> </event> </hibernate-configuration>