JPA - 找不到类型:枚举的验证器
2022-09-01 18:49:45
我有一个实体类,它使用枚举类型作为其中一个属性,当我尝试持久化实体时,我得到以下异常:
javax.validation.UnexpectedTypeException: No validator could be found for type: model.schema.BaseYesNo
任何想法为什么会发生这种情况?我的想法是,既然它是一个枚举,它应该已经由编译器验证,所以不需要某种验证器。(代码如下)
实体属性:
@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;
}
}