JPA - 找不到类型为enum的验证器 [英] JPA - No validator could be found for type: enum

查看:178
本文介绍了JPA - 找不到类型为enum的验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体类,它对其中一个属性使用枚举类型,当我尝试持久保存实体时,我收到以下异常:

I have an entity class which uses an enum type for one of the properties, and I am getting the following exception when I try to persist the entity:

javax.validation.UnexpectedTypeException: No validator could be found for type: model.schema.BaseYesNo

知道为什么会这样吗?我的想法是,因为它是一个枚举,它应该已经由编译器验证,所以不需要某种验证器。 (以下代码)

Any idea why this might be happening? My thinking is that since it is an enum, it should already be validated by the compiler, so no need for some kind of validator. (code below)

实体属性:

@Enumerated( EnumType.STRING )
@Column( name = "seeded_flag" )
private BaseYesNo seededFlag;

public BaseYesNo getSeededFlag() {
    return this.seededFlag;
}

public void setSeededFlag( BaseYesNo seededFlag ) {
    this.seededFlag = seededFlag;
}

枚举类型的定义:

public enum BaseYesNo {
    YES( "Y" ),
    NO( "N" );

    private String yesNoVal;

    private static Map< String, BaseYesNo > stringToEnum = new HashMap< String, BaseYesNo >();

    static {
            for ( BaseYesNo byn : BaseYesNo.values() ) {
            BaseYesNo.stringToEnum.put( byn.toString(), byn );
        }
    }

    BaseYesNo( String yesNoVal ) {
        this.yesNoVal = yesNoVal;
    }

    public static BaseYesNo fromString( String dbValue ) {
        return BaseYesNo.stringToEnum.get( dbValue );
    }

    @Override
    public String toString() {
        return this.yesNoVal;
    }
}


推荐答案

我和你有同样的错误。我的问题是我错误地在我的枚举属性上放置了 @Size

I had the same error as you. The problem with mine was that I had mistakenly placed @Size on my enum property:

public class PhoneNumber {
  @Size(max=30) //ERROR: this validation annotation is what caused my error
  @Nonnull
  @Enumerated(EnumType.STRING)
  private Type type;
  @Size(max = 30)
  @Nonnull
  private String number;

  public enum Type {
    Cell, Home, Work, Other
  }
}

一旦我删除了错误的 @Size ,我的错误就消失了。

Once I removed the erroneous @Size, my error went away.

@Enumerated 对我没有任何问题,我怀疑 @Column 会不会。也许你有另外一个验证注释,就像我一样。

@Enumerated didn't cause any problems for me, and I doubt @Column would. Perhaps you had another validation annotation you skimmed over like I did.

对于我的测试,我使用的是 hibernate-validator-4.1.0-Final.jar

For my testing, I was using hibernate-validator-4.1.0-Final.jar

这篇关于JPA - 找不到类型为enum的验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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