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

<legend id='OUZ6J'><style id='OUZ6J'><dir id='OUZ6J'><q id='OUZ6J'></q></dir></style></legend>
  • <tfoot id='OUZ6J'></tfoot>
      <bdo id='OUZ6J'></bdo><ul id='OUZ6J'></ul>

    <small id='OUZ6J'></small><noframes id='OUZ6J'>

      1. 如何使用带有枚举的 Hibernate 验证注释?

        时间:2023-07-26

          <small id='7pEYx'></small><noframes id='7pEYx'>

          • <bdo id='7pEYx'></bdo><ul id='7pEYx'></ul>

              <i id='7pEYx'><tr id='7pEYx'><dt id='7pEYx'><q id='7pEYx'><span id='7pEYx'><b id='7pEYx'><form id='7pEYx'><ins id='7pEYx'></ins><ul id='7pEYx'></ul><sub id='7pEYx'></sub></form><legend id='7pEYx'></legend><bdo id='7pEYx'><pre id='7pEYx'><center id='7pEYx'></center></pre></bdo></b><th id='7pEYx'></th></span></q></dt></tr></i><div id='7pEYx'><tfoot id='7pEYx'></tfoot><dl id='7pEYx'><fieldset id='7pEYx'></fieldset></dl></div>
              • <legend id='7pEYx'><style id='7pEYx'><dir id='7pEYx'><q id='7pEYx'></q></dir></style></legend>
                  <tbody id='7pEYx'></tbody>
              • <tfoot id='7pEYx'></tfoot>
                  本文介绍了如何使用带有枚举的 Hibernate 验证注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如何使用休眠注释来验证枚举成员字段?以下方法不起作用:

                  How can I use hibernate annotations to validate an enum member field? The following does not work:

                  enum UserRole {
                     USER, ADMIN;
                  }
                  
                  class User {
                     @NotBlank //HV000030: No validator could be found for type: UserRole.
                     UserRole userRole;
                  }
                  

                  推荐答案

                  请注意,您还可以创建一个验证器来检查字符串是否是枚举的一部分.

                  Note you can also create a validator to check a String is part of an enumeration.

                  public enum UserType { PERSON, COMPANY }
                  
                  @NotNull
                  @StringEnumeration(enumClass = UserCivility.class)
                  private String title;
                  

                  <小时>

                  @Documented
                  @Constraint(validatedBy = StringEnumerationValidator.class)
                  @Target({ METHOD, FIELD, ANNOTATION_TYPE, PARAMETER, CONSTRUCTOR })
                  @Retention(RUNTIME)
                  public @interface StringEnumeration {
                  
                    String message() default "{com.xxx.bean.validation.constraints.StringEnumeration.message}";
                    Class<?>[] groups() default {};
                    Class<? extends Payload>[] payload() default {};
                  
                    Class<? extends Enum<?>> enumClass();
                  
                  }
                  

                  <小时>

                  public class StringEnumerationValidator implements ConstraintValidator<StringEnumeration, String> {
                  
                    private Set<String> AVAILABLE_ENUM_NAMES;
                  
                    @Override
                    public void initialize(StringEnumeration stringEnumeration) {
                      Class<? extends Enum<?>> enumSelected = stringEnumeration.enumClass();
                      //Set<? extends Enum<?>> enumInstances = EnumSet.allOf(enumSelected);
                      Set<? extends Enum<?>> enumInstances = Sets.newHashSet(enumSelected.getEnumConstants());
                      AVAILABLE_ENUM_NAMES = FluentIterable
                              .from(enumInstances)
                              .transform(PrimitiveGuavaFunctions.ENUM_TO_NAME)
                              .toSet();
                    }
                  
                    @Override
                    public boolean isValid(String value, ConstraintValidatorContext context) {
                      if ( value == null ) {
                        return true;
                      } else {
                        return AVAILABLE_ENUM_NAMES.contains(value);
                      }
                    }
                  
                  }
                  

                  <小时>

                  这很好,因为您不会丢失错误值"的信息.您可以收到类似


                  This is nice because you don't loose the information of the "wrong value". You can get a message like

                  值someBadUserType"不是有效的用户类型.有效的用户类型值是:人员、公司

                  The value "someBadUserType" is not a valid UserType. Valid UserType values are: PERSON, COMPANY

                  <小时>

                  编辑

                  对于那些想要非 Guava 版本的人来说,它应该与以下内容一起使用:

                  For those who want a non-Guava version it should work with something like:

                  public class StringEnumerationValidator implements ConstraintValidator<StringEnumeration, String> {
                  
                    private Set<String> AVAILABLE_ENUM_NAMES;
                  
                    public static Set<String> getNamesSet(Class<? extends Enum<?>> e) {
                       Enum<?>[] enums = e.getEnumConstants();
                       String[] names = new String[enums.length];
                       for (int i = 0; i < enums.length; i++) {
                           names[i] = enums[i].name();
                       }
                       Set<String> mySet = new HashSet<String>(Arrays.asList(names));
                       return mySet;
                    }
                  
                    @Override
                    public void initialize(StringEnumeration stringEnumeration) {
                      Class<? extends Enum<?>> enumSelected = stringEnumeration.enumClass();
                      AVAILABLE_ENUM_NAMES = getNamesSet(enumSelected);
                    }
                  
                    @Override
                    public boolean isValid(String value, ConstraintValidatorContext context) {
                      if ( value == null ) {
                        return true;
                      } else {
                        return AVAILABLE_ENUM_NAMES.contains(value);
                      }
                    }
                  
                  }
                  

                  要自定义错误消息并显示适当的值,请检查以下内容:https://stackoverflow.com/a/19833921/82609

                  And to customize the error message and display the appropriate values, check this: https://stackoverflow.com/a/19833921/82609

                  这篇关于如何使用带有枚举的 Hibernate 验证注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:JPA中@javax.persistence.Lob注解有什么意义? 下一篇:您可以将字符与 == 进行比较吗?

                  相关文章

                  最新文章

                  <small id='jFMfn'></small><noframes id='jFMfn'>

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

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