[Solved] – UnexpectedTypeException- No validator could be found for constraint

Learn to solve java exception UnexpectedTypeException while using hibernate validator for java bean validations.

1) Problem

UnexpectedTypeException error you will get when you are trying to use incorrect hibernate validator annotation on any bean property. The error stack trace looks like this.

Exception in thread "main" javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.NotBlank' validating type 'java.lang.Long'. Check configuration for 'id'
	at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getExceptionForNullValidator(ConstraintTree.java:108)
	at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getInitializedConstraintValidator(ConstraintTree.java:140)
	at org.hibernate.validator.internal.engine.constraintvalidation.SimpleConstraintTree.validateConstraints(SimpleConstraintTree.java:55)
	at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.validateConstraints(ConstraintTree.java:73)
	at org.hibernate.validator.internal.metadata.core.MetaConstraint.doValidateConstraint(MetaConstraint.java:127)
	at org.hibernate.validator.internal.metadata.core.MetaConstraint.validateConstraint(MetaConstraint.java:120)
	at org.hibernate.validator.internal.engine.ValidatorImpl.validateMetaConstraint(ValidatorImpl.java:533)
	at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForSingleDefaultGroupElement(ValidatorImpl.java:496)
	at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForDefaultGroup(ValidatorImpl.java:465)
	at org.hibernate.validator.internal.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:430)
	at org.hibernate.validator.internal.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:380)
	at org.hibernate.validator.internal.engine.ValidatorImpl.validate(ValidatorImpl.java:169)
	at com.howtodoinjava.example.TestHibernateValidator.main(TestHibernateValidator.java:25)

For example above error is received you you try to put @NotBlank annotation on any non-string type field e.g. java.lang.Long type.

public class User {

    @NotBlank			//Incorrect usage
    private Long id;

    //More fields
 }

Read More : Hibernate Validator

2) Solution

As stated in problem, to solve this error you MUST use correct annotations. In above problem, @NotBlank annotation must be applied on any String field only.

To validate long type field, use annotation @NotNull.

public class User {

public class User {

    @NotNull(message = "Please enter id")
    private Long id;

    @Size(max = 20, min = 3, message = "{user.name.invalid}")
    @NotEmpty(message = "Please enter name")
    private String name;

    //More fields
 }

Drop me your questions in comments section.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

3 thoughts on “[Solved] – UnexpectedTypeException- No validator could be found for constraint”

  1. What could be a problem with String fields as following:

     
        @NotEmpty
        @Length(max = 512)
       private String shortDescription;
    

    I got an error:

    HV000030: No validator could be found for constraint ‘javax.validation.constraints.NotEmpty’ validating type ‘java.lang.String’ .Checkconfiguration for ‘updateContest.arg1.shortDescription’

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.