程序化约束定义并重新定义默认组

时间:2014-07-28 11:52:39

标签: java hibernate validation bean-validation hibernate-validator

我想使用编程约束定义来重新定义默认组:

constraintMapping.type(User.class)
   .defaultGroupSequence(AlwaysCheck.class, User.class)
   .property("name", ElementType.FIELD)
   .constraint(
        new LengthDef().max(1)
        .groups(AlwaysCheck.class)
        .message("text"))

当我将对象保留在数据库中时,检查没有发生。

在注释中给出相同的约束:

@GroupSequence({AlwaysCheck.class, User.class})
public class User implements Serializable {
    //...
    @Length(max = 1, message = "text", groups = {AlwaysCheck.class})         
    private String name;
}

我如何将其表达为一个程序?

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
   ...
    <property name="hibernateProperties">
        <props>                                
       ...
            <prop key="javax.persistence.validation.factory">#{validator}</prop>                
        </props>
    </property>                
  </bean>

UPDATE2 @Gunnar

在Spring上下文中我创建了bean

       <bean id="validator" class="....LocalValidatorFactoryBeanOverride">          
           <property name="validationMessageSource" ref="messageSource"/>                                  
           <property name="constraintMappingFactory" ref="constraintMappingFactoryComponent" />                    
       </bean>

     //...
     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 && 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<String, String> 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());
}

   }

1 个答案:

答案 0 :(得分:2)

您是否在引导验证工厂时添加了您创建的映射?它需要像这样完成:

Validator validator = configuration.addMapping( constraintMapping )
    .buildValidatorFactory()
    .getValidator();