title: programmatic constraint definition and redefine the default group
body: <p>I want to use a programmatic constraint definition to redefine the default group:</p>
  
  <pre><code>constraintMapping.type(User.class)
     .defaultGroupSequence(AlwaysCheck.class, User.class)
     .property("name", ElementType.FIELD)
     .constraint(
          new LengthDef().max(1)
          .groups(AlwaysCheck.class)
          .message("text"))
  </code></pre>
  
  <p>When I persist the object in the database the check is not happening.</p>
  
  <p>The same constraint works when given in annotations:</p>
  
  <pre><code>@GroupSequence({AlwaysCheck.class, User.class})
  public class User implements Serializable {
      //...
      @Length(max = 1, message = "text", groups = {AlwaysCheck.class})         
      private String name;
  }
  </code></pre>
  
  <p>How can I express this as a program?</p>
  
  <p><strong>SOLUTION</strong></p>
  
  <pre><code>&lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" &gt;
     ...
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;                                
         ...
              &lt;prop key="javax.persistence.validation.factory"&gt;#{validator}&lt;/prop&gt;                
          &lt;/props&gt;
      &lt;/property&gt;                
    &lt;/bean&gt;
  </code></pre>
  
  <p><strong>UPDATE2 @Gunnar</strong></p>
  
  <p>In Spring Context I create bean</p>
  
  <pre><code>       &lt;bean id="validator" class="....LocalValidatorFactoryBeanOverride"&gt;          
             &lt;property name="validationMessageSource" ref="messageSource"/&gt;                                  
             &lt;property name="constraintMappingFactory" ref="constraintMappingFactoryComponent" /&gt;                    
         &lt;/bean&gt;
  
       //...
       public class LocalValidatorFactoryBeanOverride extends SpringValidatorAdapterOverride
      implements ValidatorFactory, ApplicationContextAware, InitializingBean {
  
     //UPDATE My constraint definition
     private ConstraintMappingFactory constraintMappingFactory;
  
     //..
     @SuppressWarnings("unchecked")
  public void afterPropertiesSet() {
      @SuppressWarnings("rawtypes")
      Configuration configuration = (this.providerClass != null ?
              Validation.byProvider(this.providerClass).configure() :
              Validation.byDefaultProvider().configure());
  
      MessageInterpolator targetInterpolator = this.messageInterpolator;
      if (targetInterpolator == null) {
          targetInterpolator = configuration.getDefaultMessageInterpolator();
      }
      configuration.messageInterpolator(new LocaleContextMessageInterpolator(targetInterpolator));
  
      if (this.traversableResolver != null) {
          configuration.traversableResolver(this.traversableResolver);
      }
  
      ConstraintValidatorFactory targetConstraintValidatorFactory = this.constraintValidatorFactory;
      if (targetConstraintValidatorFactory == null &amp;&amp; this.applicationContext != null) {
          targetConstraintValidatorFactory =
                  new SpringConstraintValidatorFactory(this.applicationContext.getAutowireCapableBeanFactory());
      }
      if (targetConstraintValidatorFactory != null) {
          configuration.constraintValidatorFactory(targetConstraintValidatorFactory);
      }
  
      if (this.mappingLocations != null) {
          for (Resource location : this.mappingLocations) {
              try {
                  configuration.addMapping(location.getInputStream());
              }
              catch (IOException ex) {
                  throw new IllegalStateException("Cannot read mapping resource: " + location);
              }
          }
      }
  
      for (Map.Entry&lt;String, String&gt; entry : this.validationPropertyMap.entrySet()) {
          configuration.addProperty(entry.getKey(), entry.getValue());
      }
  
              if(constraintMappingFactory != null) {
  
                  if(configuration instanceof HibernateValidatorConfiguration) {
  
                      HibernateValidatorConfiguration hibernateValidatorConfiguration = (HibernateValidatorConfiguration) configuration;
  
                      ConstraintMapping constraintMapping = constraintMappingFactory.getConstraintMapping(hibernateValidatorConfiguration);                        
                      hibernateValidatorConfiguration.addMapping(constraintMapping);
                  }                    
              }
  
  
  
      this.validatorFactory = configuration.buildValidatorFactory();                
      setTargetValidator(this.validatorFactory.getValidator());
  }
  
     }
  </code></pre>