代码之家  ›  专栏  ›  技术社区  ›  Don Rhummy

使用Hibernate 6.0.10验证会导致每个模式的ConstraintDefinitionException

  •  2
  • Don Rhummy  · 技术社区  · 6 年前

    @EachPattern 列表中包含:

    @EachPattern(
       regexp = "^[a-z]+",
       message = "Invalid"
    )
    public List<String> getTestItems()
    {
        return Arrays.asList( "aaaaa" );
    }
    

    Caused by: javax.validation.ConstraintDefinitionException: HV000243: Constraint cz.jirutka.validator.collection.constraints.EachPattern references constraint validator type cz.jirutka.validator.collection.CommonEachValidator, but this validator is defined for constraint type java.lang.annotation.Annotation.
        at org.hibernate.validator.internal.engine.constraintvalidation.ClassBasedValidatorDescriptor.of(ClassBasedValidatorDescriptor.java:48)
        at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorDescriptor.forClass(ConstraintValidatorDescriptor.java:49)
        at org.hibernate.validator.internal.metadata.core.ConstraintHelper.lambda$getDefaultValidatorDescriptors$6(ConstraintHelper.java:1001)
        at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
        at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
        at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
        at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
        at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
        at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
        at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
        at org.hibernate.validator.internal.metadata.core.ConstraintHelper.getDefaultValidatorDescriptors(ConstraintHelper.java:1002)
        at org.hibernate.validator.internal.metadata.core.ConstraintHelper.lambda$getAllValidatorDescriptors$2(ConstraintHelper.java:730)
        at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
        at org.hibernate.validator.internal.metadata.core.ConstraintHelper$ValidatorDescriptorMap.computeIfAbsent(ConstraintHelper.java:1043)
        at org.hibernate.validator.internal.metadata.core.ConstraintHelper$ValidatorDescriptorMap.access$100(ConstraintHelper.java:1033)
        at org.hibernate.validator.internal.metadata.core.ConstraintHelper.getAllValidatorDescriptors(ConstraintHelper.java:730)
        at org.hibernate.validator.internal.metadata.core.ConstraintHelper.findValidatorDescriptors(ConstraintHelper.java:744)
        at org.hibernate.validator.internal.metadata.core.ConstraintHelper.assertValidationAppliesToParameterSetUpCorrectly(ConstraintHelper.java:923)
        at org.hibernate.validator.internal.metadata.core.ConstraintHelper.lambda$isConstraintAnnotation$5(ConstraintHelper.java:864)
        at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
        at org.hibernate.validator.internal.metadata.core.ConstraintHelper.isConstraintAnnotation(ConstraintHelper.java:860)
        at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.findConstraintAnnotations(AnnotationMetaDataProvider.java:507)
        at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.findConstraints(AnnotationMetaDataProvider.java:461)
        at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.findExecutableMetaData(AnnotationMetaDataProvider.java:302)
        at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getMetaData(AnnotationMetaDataProvider.java:285)
        at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getMethodMetaData(AnnotationMetaDataProvider.java:272)
        at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.retrieveBeanConfiguration(AnnotationMetaDataProvider.java:134)
        at org.hibernate.validator.internal.metadata.provider.AnnotationMetaDataProvider.getBeanConfiguration(AnnotationMetaDataProvider.java:124)
        at org.hibernate.validator.internal.metadata.BeanMetaDataManager.getBeanConfigurationForHierarchy(BeanMetaDataManager.java:220)
        at org.hibernate.validator.internal.metadata.BeanMetaDataManager.createBeanMetaData(BeanMetaDataManager.java:187)
        at org.hibernate.validator.internal.metadata.BeanMetaDataManager.lambda$getBeanMetaData$0(BeanMetaDataManager.java:160)
        at java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:324)
        at org.hibernate.validator.internal.metadata.BeanMetaDataManager.getBeanMetaData(BeanMetaDataManager.java:159)
        at org.hibernate.validator.internal.engine.ValidatorImpl.buildNewLocalExecutionContext(ValidatorImpl.java:749)
        at org.hibernate.validator.internal.engine.ValidatorImpl.access$200(ValidatorImpl.java:85)
        at org.hibernate.validator.internal.engine.ValidatorImpl$CascadingValueReceiver.doValidate(ValidatorImpl.java:681)
        at org.hibernate.validator.internal.engine.ValidatorImpl$CascadingValueReceiver.indexedValue(ValidatorImpl.java:657)
        at org.hibernate.validator.internal.engine.valueextraction.ListValueExtractor.extractValues(ListValueExtractor.java:26)
        at org.hibernate.validator.internal.engine.valueextraction.ListValueExtractor.extractValues(ListValueExtractor.java:16)
        at org.hibernate.validator.internal.engine.valueextraction.ValueExtractorHelper.extractValues(ValueExtractorHelper.java:42)
        at org.hibernate.validator.internal.engine.ValidatorImpl.validateCascadedContainerElementsForCurrentGroup(ValidatorImpl.java:627)
        at org.hibernate.validator.internal.engine.ValidatorImpl.validateCascadedConstraints(ValidatorImpl.java:576)
        at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:389)
        at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:169)
    

    @Constraint(validatedBy = CommonEachValidator.class) public class CommonEachValidator implements ConstraintValidator<Annotation, Collection<?>> 这似乎造成了问题。

    compile("org.hibernate:hibernate-core:5.0.12.Final")
    compile("org.hibernate:hibernate-entitymanager:5.0.12.Final")
    compile("org.hibernate:hibernate-validator:6.0.10.Final")
    compile("javax.validation:validation-api:2.0.1.Final")
    

    1 回复  |  直到 6 年前
        1
  •  3
  •   streetturtle    6 年前

    including

    所以你可以移除 cz.jirutka.validator.collection.constraints.EachPattern javax.validation.constraints '@模式如下:

    public List<@Pattern(regexp = "^[a-z]+", message = "Invalid") String> getTestItems()
    {
        return Arrays.asList( "aaaaa" );
    }