14

有没有一种方法可以使用此处定义的注释来定义 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 可以为空,或者 fieldB 可以为空,但不能同时为空。

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

4

3 回答 3

17

我终于写了整个验证器:

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) {
                 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.
}
于 2012-08-31T10:09:07.573 回答
5

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

概念:

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

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

在那里我使用了commons-beanutils和的方法commons-collections

于 2012-08-31T09:03:23.217 回答
0

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

验证注释类将如下所示。

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
}
于 2019-09-26T05:27:17.840 回答