• <small id='mtxrR'></small><noframes id='mtxrR'>

        <bdo id='mtxrR'></bdo><ul id='mtxrR'></ul>

      1. <tfoot id='mtxrR'></tfoot>
      2. <legend id='mtxrR'><style id='mtxrR'><dir id='mtxrR'><q id='mtxrR'></q></dir></style></legend>
        <i id='mtxrR'><tr id='mtxrR'><dt id='mtxrR'><q id='mtxrR'><span id='mtxrR'><b id='mtxrR'><form id='mtxrR'><ins id='mtxrR'></ins><ul id='mtxrR'></ul><sub id='mtxrR'></sub></form><legend id='mtxrR'></legend><bdo id='mtxrR'><pre id='mtxrR'><center id='mtxrR'></center></pre></bdo></b><th id='mtxrR'></th></span></q></dt></tr></i><div id='mtxrR'><tfoot id='mtxrR'></tfoot><dl id='mtxrR'><fieldset id='mtxrR'></fieldset></dl></div>

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

        时间:2023-09-28

            <tbody id='BynRW'></tbody>
          <tfoot id='BynRW'></tfoot>
        1. <small id='BynRW'></small><noframes id='BynRW'>

          <i id='BynRW'><tr id='BynRW'><dt id='BynRW'><q id='BynRW'><span id='BynRW'><b id='BynRW'><form id='BynRW'><ins id='BynRW'></ins><ul id='BynRW'></ul><sub id='BynRW'></sub></form><legend id='BynRW'></legend><bdo id='BynRW'><pre id='BynRW'><center id='BynRW'></center></pre></bdo></b><th id='BynRW'></th></span></q></dt></tr></i><div id='BynRW'><tfoot id='BynRW'></tfoot><dl id='BynRW'><fieldset id='BynRW'></fieldset></dl></div>
              1. <legend id='BynRW'><style id='BynRW'><dir id='BynRW'><q id='BynRW'></q></dir></style></legend>
                  <bdo id='BynRW'></bdo><ul id='BynRW'></ul>

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

                  问题描述

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

                  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 可以为 null 或 fieldB 可以为 null,但不能同时为 null.

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

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

                  上一篇:@autowired on 方法在 Spring 下一篇:Spring,使用 @Configuration 和 @Bean 注解

                  相关文章

                  最新文章

                • <small id='ce4iQ'></small><noframes id='ce4iQ'>

                    <legend id='ce4iQ'><style id='ce4iQ'><dir id='ce4iQ'><q id='ce4iQ'></q></dir></style></legend>

                    <tfoot id='ce4iQ'></tfoot>

                        <bdo id='ce4iQ'></bdo><ul id='ce4iQ'></ul>
                    1. <i id='ce4iQ'><tr id='ce4iQ'><dt id='ce4iQ'><q id='ce4iQ'><span id='ce4iQ'><b id='ce4iQ'><form id='ce4iQ'><ins id='ce4iQ'></ins><ul id='ce4iQ'></ul><sub id='ce4iQ'></sub></form><legend id='ce4iQ'></legend><bdo id='ce4iQ'><pre id='ce4iQ'><center id='ce4iQ'></center></pre></bdo></b><th id='ce4iQ'></th></span></q></dt></tr></i><div id='ce4iQ'><tfoot id='ce4iQ'></tfoot><dl id='ce4iQ'><fieldset id='ce4iQ'></fieldset></dl></div>