休眠的验证注解 - 验证至少一个字段不为空 [英] Hibernate validation annotation - validate that at least one field is not null

查看:925
本文介绍了休眠的验证注解 - 验证至少一个字段不为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法来定义使用注释Hibernate的验证规则定义的<一个href=\"http://docs.jboss.org/hibernate/validator/4.0.1/reference/en/html_single/#validator-customconstraints-compound\"相对=nofollow>这里,指出至少有一个字段不得为空?

Is there a way to define a Hibernate validation rule using annotations as defined here, stating that at least one field shall be not null?

这是一个假设的例子( @OneFieldMustBeNotNullConstraint 实际上并不存在):

This would be a hypothetical example (@OneFieldMustBeNotNullConstraint does not really exist):

@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可以为空,但不能同时使用。

In the illustrated case, fieldA can be null or fieldB can be null, but not both.

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

One way would be to create my own validator, but I'd like to avoid if it already exists. Please share one validator if you have one already made... thanks!

推荐答案

我终于写出了整个验证:

I finally wrote the whole validator:

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.
}

这篇关于休眠的验证注解 - 验证至少一个字段不为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆