ORM integration - Hibernate event-based validation
6.3.1. Hibernate event-based validation Hibernate Validator has a built-in Hibernate event listener - org.hibernate.cfg.beanvalidation.BeanValidationEventListener - which is part of Hibernate Annotations (as of Hibernate 3.5.x). Whenever a PreInsertEvent, PreUpdateEvent or PreDeleteEvent occurs, the listener will verify all constraints of the entity instance and throw an exception if any constraint is violated. Per default objects will be checked before any inserts or updates are made by Hibernate. Pre deletion events will per default not trigger a validation. You can configure the groups to be validated per event type using the properties javax.persistence.validation.group.pre-persist, javax.persistence.validation.group.pre-update and javax.persistence.validation.group.pre-remove. The values of these properties are the comma-separated, fully specified class names of the groups to validate. Example 6.1, “Manual configuration of BeanValidationEvenListener” shows the default values for these properties. In this case they could also be omitted. On constraint violation, the event will raise a runtime ConstraintViolationException which contains a set of ConstraintViolations describing each failure. If Hibernate Validator is present in the classpath, Hibernate Annotations (or Hibernate EntityManager) will use it transparently. To avoid validation even though Hibernate Validator is in the classpath set javax.persistence.validation.mode to none. 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: Example 6.1. Manual configuration of BeanValidationEvenListener ... javax.validation.groups.Default javax.validation.groups.Default ...