gpt4 book ai didi

java - 编程约束定义并重新定义默认组

转载 作者:行者123 更新时间:2023-11-30 09:04:59 25 4
gpt4 key购买 nike

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

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());
}

}

最佳答案

您是否添加了在引导 validator 工厂时创建的映射?需要这样做:

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

关于java - 编程约束定义并重新定义默认组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24994652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com