Search code examples
springhibernatebean-validationhibernate-validator

Does Spring's validation routine support Hibernate's JSR 303 implementation option for fail fast mode?


8.2. Fail fast mode

I'm looking for where I might configure this option in Spring, but I'm not sure this is a part of JSR 303 so much as it is a Hibernate configuration for their own validator implementation. This is important because if I have multiple constraint violations I only want the first one "thrown".


Solution

  • Assuming you're using Spring's LocalValidatorFactoryBean to set up your validator, you can specify provider-specific configuration properties within the validationPropertyMap attribute.

    The property name of Hibernate Validator's fail-fast attribute is "hibernate.validator.fail_fast", so you would set up your validator like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:util="http://www.springframework.org/schema/util"
      xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">
    
      <bean name="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
        <property name="validationPropertyMap">
          <util:map>
            <entry key="hibernate.validator.fail_fast" value="true"/>
          </util:map>
        </property>
      </bean>
    </beans>