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

时间:2012-08-31 08:38:30

标签: java hibernate annotations validation

有没有办法使用定义here的注释来定义Hibernate验证规则,说明至少有一个字段不为空?

这是一个假设的例子(@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,但不能同时为。

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

3 个答案:

答案 0 :(得分: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.
}

答案 1 :(得分:3)

只需编写自己的验证器即可。是否应该非常简单:迭代字段名称并使用反射获取字段值。

概念:

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

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

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

答案 2 :(得分: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
}