Hibernate验证注释-验证至少一个字段不为空

问题描述 投票:10回答:3

是否有一种方法可以使用定义为here的注释来定义一个Hibernate验证规则,说明至少一个字段不应为null?

这将是一个假设的示例(@OneFieldMustBeNotNullConstraint确实不存在:]

@Entity
@OneFieldMustBeNotNullConstraint(list={fieldA,fieldB})
public class Card {

    @Id
    @GeneratedValue
    private Integer card_id;

    @Column(nullable = true)
    private Long fieldA;

    @Column(nullable = true)
    private Long fieldB;

}

在所示的情况下,fieldA可以为null或fieldB可以为null,但不能同时为两者。

一种方法是创建我自己的验证器,但是我想避免它已经存在。如果您已经验证过,请共享一个验证人...谢谢!

java hibernate annotations validation
3个回答
14
投票

我终于写了整个验证器:

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;

import org.apache.commons.beanutils.PropertyUtils; 

@Target( { TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = CheckAtLeastOneNotNull.CheckAtLeastOneNotNullValidator.class)
@Documented
public @interface CheckAtLeastOneNotNull {

     String message() default "{com.xxx.constraints.checkatleastnotnull}";

        Class<?>[] groups() default {};

        Class<? extends Payload>[] payload() default {};

        String[] fieldNames();

        public static class CheckAtLeastOneNotNullValidator implements ConstraintValidator<CheckAtLeastOneNotNull, Object> {

            private String[] fieldNames;

            public void initialize(CheckAtLeastOneNotNull constraintAnnotation) {
                this.fieldNames = constraintAnnotation.fieldNames();
            }

            public boolean isValid(Object object, ConstraintValidatorContext constraintContext) {


                if (object == null)
                    return true;

                try {

                    for (String fieldName:fieldNames){
                        Object property = PropertyUtils.getProperty(object, fieldName);

                        if (property!=null) return true;
                    }

                    return false;

                } catch (Exception e) {
                   System.printStackTrace(e);   
                    return false;
                }
            }

        }

}

用法示例:

@Entity
@CheckAtLeastOneNotNull(fieldNames={"fieldA","fieldB"})
public class Reward {

    @Id
    @GeneratedValue
    private Integer id;

    private Integer fieldA;
    private Integer fieldB;

    [...] // accessors, other fields, etc.
}

3
投票

只需编写您自己的验证器。不是应该很简单:遍历字段名称并使用反射来获取字段值。

概念:

Collection<String> values = Arrays.asList(
    BeanUtils.getProperty(obj, fieldA),
    BeanUtils.getProperty(obj, fieldB),
);

return CollectionUtils.exists(values, PredicateUtils.notNullPredicate());

我使用了commons-beanutilscommons-collections中的方法。


0
投票

这有点像Resh32的答案,但这也会将验证消息与已验证对象的特定字段绑定。

验证批注类将类似于以下类。

import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
 * @author rumman
 * @since 9/23/19
 */
@Target(TYPE)
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = NotNullAnyValidator.class)
public @interface NotNullAny {

    String[] fieldNames();

    String errorOnProperty();

    String messageKey() default "{error.required}";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};
}

验证器类将如下所示。

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Objects;

import static org.springframework.beans.BeanUtils.getPropertyDescriptor;

/**
 * @author rumman
 * @since 9/23/19
 */
public class NotNullAnyValidator implements ConstraintValidator<NotNullAny, Object> {

    private String[] fieldNames;
    private String errorOnProperty;
    private String messageKey;

    @Override
    public void initialize(NotNullAny validateDateRange) {
        fieldNames = validateDateRange.fieldNames();
        errorOnProperty = validateDateRange.errorOnProperty();
        messageKey = validateDateRange.messageKey();
    }

    @Override
    public boolean isValid(Object obj, ConstraintValidatorContext validatorContext) {

        Object[] fieldValues = new Object[fieldNames.length];

        try {
            for (int i = 0; i < fieldValues.length; i++) {
                fieldValues[i] = getPropertyDescriptor(obj.getClass(), fieldNames[i]).getReadMethod().invoke(obj);
            }

        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }

        if (Arrays.stream(fieldValues).noneMatch(Objects::nonNull)) {
            validatorContext.buildConstraintViolationWithTemplate(messageKey)
                    .addPropertyNode(errorOnProperty)
                    .addConstraintViolation()
                    .disableDefaultConstraintViolation();

            return false;
        }

        return true;
    }
}

请注意最后一个if条件块,这将检查是否未找到非null值,然后指定错误消息,错误消息将绑定到的属性并添加约束违反。

要在课堂上使用注释

/**
 * @author rumman
 * @since 9/23/19
 */
@NotNullAny(fieldNames = {"field1", "field2", "field3"},
        errorOnProperty = "field1",
        messageKey = "my.error.msg.key")
public class TestEntityForValidation {

    private String field1;
    private String field2;
    private String field3;

    // standard constructor(s) and getter & setters below
}
© www.soinside.com 2019 - 2024. All rights reserved.