Hibernate Validator and Spring

If you  cannot or don't want to rely on auto-configuration of Hibernate  Validator, for example in a JPA 1 environment and you are using Spring,  all one needs to do is this:
<bean id="validationFactory" class="javax.validation.Validation"
          factory-method="buildDefaultValidatorFactory" />
    
<bean id="beanValidationEventListener" class="org.hibernate.cfg.beanvalidation.BeanValidationEventListener">
   <constructor-arg index="0" ref="validationFactory" />
   <constructor-arg index="1">
   <props/>
   </constructor-arg>
</bean>

To wire these beans into the sessionfactory in hibernate, this  is all that needs to be done given the other bean definitions:

 

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="eventListeners">
    <map>
       <entry key="pre-insert" value-ref="beanValidationEventListener" />
       <entry key="pre-update" value-ref="beanValidationEventListener" />
    </map>
    </property>
    ....
</bean>