Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA - No validator could be found for type: enum

Tags:

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)

The entity property:

@Enumerated( EnumType.STRING ) @Column( name = "seeded_flag" ) private BaseYesNo seededFlag;  public BaseYesNo getSeededFlag() {     return this.seededFlag; }  public void setSeededFlag( BaseYesNo seededFlag ) {     this.seededFlag = seededFlag; } 

And the definition of the enum type:

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;     } } 
like image 212
GreenSaguaro Avatar asked Aug 04 '11 06:08

GreenSaguaro


People also ask

What are enum types?

Enumerated (enum) types are data types that comprise a static, ordered set of values. They are equivalent to the enum types supported in a number of programming languages. An example of an enum type might be the days of the week, or a set of status values for a piece of data.

How do you check if a string is present in an enum Java?

Then you can just do: values. contains("your string") which returns true or false.

What is Java enum?

An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables). To create an enum , use the enum keyword (instead of class or interface), and separate the constants with a comma.


2 Answers

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

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

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

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

like image 180
pgreen2 Avatar answered Sep 20 '22 17:09

pgreen2


I came across the same situation, but with the message No validator could be found for type int. searching the web I found some solutions, most of them focus on changing type int to type Integer, because type Integer accept nulls. like in here : Validation - Empty int field

unfortunately that didn't work for me. but when I substituted @size with @Min and @Max everything went great. hoping this may gives you a hand.

like image 41
khldqr Avatar answered Sep 17 '22 17:09

khldqr