[java] Validation error: "No validator could be found for type: java.lang.Integer"

I am working on a project with Spring why do I keep getting the following error?

javax.validation.UnexpectedTypeException:
No validator could be found for type: java.lang.Integer


Here is my code:

package com.s2rsolutions.model;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.NotEmpty;

@Entity
@Table(name = "sales")
public class Sales {

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "ttl_d_sls_lst_mth", nullable = false)
    private Integer ttl_d_sls_lst_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "ttl_d_sls_6_mth", nullable = false)
    private Integer ttl_d_sls_6_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "ttl_d_sls_mtht", nullable = false)
    private Integer ttl_d_sls_mtht;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "ttl_s_sls_lst_mth", nullable = false)
    private Integer ttl_s_sls_lst_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "ttl_s_sls_6_mth", nullable = false)
    private Integer ttl_s_sls_6_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "ttl_s_sls_mtht", nullable = false)
    private Integer ttl_s_sls_mtht;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "nw_lst_mth", nullable = false)
    private Integer nw_lst_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "nw_6_mth", nullable = false)
    private Integer nw_6_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "nw_mtht", nullable = false)
    private Integer nw_mtht;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "usd_lst_mth", nullable = false)
    private Integer usd_lst_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "usd_6_mth", nullable = false)
    private Integer usd_6_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "usd_mtht", nullable = false)
    private Integer usd_mtht;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "grss_prt_pvnr_lst_mth", nullable = false)
    private Integer grss_prt_pvnr_lst_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "grss_prt_pvnr_6_mth", nullable = false)
    private Integer grss_prt_pvnr_6_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "grss_prt_pvnr_mtht", nullable = false)
    private Integer grss_prt_pvnr_mtht;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "grss_prt_pvur_lst_mth", nullable = false)
    private Integer grss_prt_pvur_lst_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "grss_prt_pvur_6_mth", nullable = false)
    private Integer grss_prt_pvur_6_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "grss_prt_pvur_mtht", nullable = false)
    private Integer grss_prt_pvur_mtht;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "shwrm_tffic_lst_mth", nullable = false)
    private Integer shwrm_tffic_lst_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "shwrm_tffic_6_mth", nullable = false)
    private Integer shwrm_tffic_6_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "shwrm_tffic_mtht", nullable = false)
    private Integer shwrm_tffic_mtht;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "shwrm_clsng_lst_mth", nullable = false)
    private Integer shwrm_clsng_lst_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "shwrm_clsng_6_mth", nullable = false)
    private Integer shwrm_clsng_6_mth;

    @NotEmpty(message = "The above field must not be blank.")
    @Column(name = "shwrm_clsng_mtht", nullable = false)
    private Integer shwrm_clsng_mtht;

    @Column(name = "date_added")
    private Date addedDate;


    public Integer getTtl_d_sls_lst_mth() {
        return ttl_d_sls_lst_mth;
    }

    public void setTtl_d_sls_lst_mth(Integer ttl_d_sls_lst_mth) {
        this.ttl_d_sls_lst_mth = ttl_d_sls_lst_mth;
    }

    public Integer getTtl_d_sls_6_mth() {
        return ttl_d_sls_6_mth;
    }

    public void setTtl_d_sls_6_mth(Integer ttl_d_sls_6_mth) {
        this.ttl_d_sls_6_mth = ttl_d_sls_6_mth;
    }

    public Integer getTtl_d_sls_mtht() {
        return ttl_d_sls_mtht;
    }

    public void setTtl_d_sls_mtht(Integer ttl_d_sls_mtht) {
        this.ttl_d_sls_mtht = ttl_d_sls_mtht;
    }

    public Integer getTtl_s_sls_lst_mth() {
        return ttl_s_sls_lst_mth;
    }

    public void setTtl_s_sls_lst_mth(Integer ttl_s_sls_lst_mth) {
        this.ttl_s_sls_lst_mth = ttl_s_sls_lst_mth;
    }

    public Integer getTtl_s_sls_6_mth() {
        return ttl_s_sls_6_mth;
    }

    public void setTtl_s_sls_6_mth(Integer ttl_s_sls_6_mth) {
        this.ttl_s_sls_6_mth = ttl_s_sls_6_mth;
    }

    public Integer getTtl_s_sls_mtht() {
        return ttl_s_sls_mtht;
    }

    public void setTtl_s_sls_mtht(Integer ttl_s_sls_mtht) {
        this.ttl_s_sls_mtht = ttl_s_sls_mtht;
    }

    public Integer getNw_lst_mth() {
        return nw_lst_mth;
    }

    public void setNw_lst_mth(Integer nw_lst_mth) {
        this.nw_lst_mth = nw_lst_mth;
    }

    public Integer getNw_6_mth() {
        return nw_6_mth;
    }

    public void setNw_6_mth(Integer nw_6_mth) {
        this.nw_6_mth = nw_6_mth;
    }

    public Integer getNw_mtht() {
        return nw_mtht;
    }

    public void setNw_mtht(Integer nw_mtht) {
        this.nw_mtht = nw_mtht;
    }

    public Integer getUsd_lst_mth() {
        return usd_lst_mth;
    }

    public void setUsd_lst_mth(Integer usd_lst_mth) {
        this.usd_lst_mth = usd_lst_mth;
    }

    public Integer getUsd_6_mth() {
        return usd_6_mth;
    }

    public void setUsd_6_mth(Integer usd_6_mth) {
        this.usd_6_mth = usd_6_mth;
    }

    public Integer getUsd_mtht() {
        return usd_mtht;
    }

    public void setUsd_mtht(Integer usd_mtht) {
        this.usd_mtht = usd_mtht;
    }

    public Integer getGrss_prt_pvnr_lst_mth() {
        return grss_prt_pvnr_lst_mth;
    }

    public void setGrss_prt_pvnr_lst_mth(Integer grss_prt_pvnr_lst_mth) {
        this.grss_prt_pvnr_lst_mth = grss_prt_pvnr_lst_mth;
    }

    public Integer getGrss_prt_pvnr_6_mth() {
        return grss_prt_pvnr_6_mth;
    }

    public void setGrss_prt_pvnr_6_mth(Integer grss_prt_pvnr_6_mth) {
        this.grss_prt_pvnr_6_mth = grss_prt_pvnr_6_mth;
    }

    public Integer getGrss_prt_pvnr_mtht() {
        return grss_prt_pvnr_mtht;
    }

    public void setGrss_prt_pvnr_mtht(Integer grss_prt_pvnr_mtht) {
        this.grss_prt_pvnr_mtht = grss_prt_pvnr_mtht;
    }

    public Integer getGrss_prt_pvur_lst_mth() {
        return grss_prt_pvur_lst_mth;
    }

    public void setGrss_prt_pvur_lst_mth(Integer grss_prt_pvur_lst_mth) {
        this.grss_prt_pvur_lst_mth = grss_prt_pvur_lst_mth;
    }

    public Integer getGrss_prt_pvur_6_mth() {
        return grss_prt_pvur_6_mth;
    }

    public void setGrss_prt_pvur_6_mth(Integer grss_prt_pvur_6_mth) {
        this.grss_prt_pvur_6_mth = grss_prt_pvur_6_mth;
    }

    public Integer getGrss_prt_pvur_mtht() {
        return grss_prt_pvur_mtht;
    }

    public void setGrss_prt_pvur_mtht(Integer grss_prt_pvur_mtht) {
        this.grss_prt_pvur_mtht = grss_prt_pvur_mtht;
    }

    public Integer getShwrm_tffic_lst_mth() {
        return shwrm_tffic_lst_mth;
    }

    public void setShwrm_tffic_lst_mth(Integer shwrm_tffic_lst_mth) {
        this.shwrm_tffic_lst_mth = shwrm_tffic_lst_mth;
    }

    public Integer getShwrm_tffic_6_mth() {
        return shwrm_tffic_6_mth;
    }

    public void setShwrm_tffic_6_mth(Integer shwrm_tffic_6_mth) {
        this.shwrm_tffic_6_mth = shwrm_tffic_6_mth;
    }

    public Integer getShwrm_tffic_mtht() {
        return shwrm_tffic_mtht;
    }

    public void setShwrm_tffic_mtht(Integer shwrm_tffic_mtht) {
        this.shwrm_tffic_mtht = shwrm_tffic_mtht;
    }

    public Integer getShwrm_clsng_lst_mth() {
        return shwrm_clsng_lst_mth;
    }

    public void setShwrm_clsng_lst_mth(Integer shwrm_clsng_lst_mth) {
        this.shwrm_clsng_lst_mth = shwrm_clsng_lst_mth;
    }

    public Integer getShwrm_clsng_6_mth() {
        return shwrm_clsng_6_mth;
    }

    public void setShwrm_clsng_6_mth(Integer shwrm_clsng_6_mth) {
        this.shwrm_clsng_6_mth = shwrm_clsng_6_mth;
    }

    public Integer getShwrm_clsng_mtht() {
        return shwrm_clsng_mtht;
    }

    public void setShwrm_clsng_mtht(Integer shwrm_clsng_mtht) {
        this.shwrm_clsng_mtht = shwrm_clsng_mtht;
    }

    public Date getAddedDate() {
        return addedDate;
    }

    public void setAddedDate(Date addedDate) {
        this.addedDate = addedDate;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Id
    @Column(name = "username")
    private String username;

}

This question is related to java hibernate validation spring-mvc

The answer is


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

To validate long type field, use annotation @NotNull.


You can add hibernate validator dependency, to provide a Validator

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.0.12.Final</version>
</dependency>

As per the javadoc of NotEmpty, Integer is not a valid type for it to check. It's for Strings and collections. If you just want to make sure an Integer has some value, javax.validation.constraints.NotNull is all you need.

public @interface NotEmpty

Asserts that the annotated string, collection, map or array is not null or empty.


For this type error: UnexpectedTypeException ERROR: We are trying to use incorrect Hibernate validator annotation on any bean property. For this same issue for my Springboot project( validating type 'java.lang.Integer')

The solution that worked for me is using @NotNull for Integer.


As the question is asked simply use @Min(1) instead of @size on integer fields and it will work.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to hibernate

Hibernate Error executing DDL via JDBC Statement How does spring.jpa.hibernate.ddl-auto property exactly work in Spring? Error creating bean with name 'entityManagerFactory' defined in class path resource : Invocation of init method failed JPA Hibernate Persistence exception [PersistenceUnit: default] Unable to build Hibernate SessionFactory Disable all Database related auto configuration in Spring Boot Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] HikariCP - connection is not available Hibernate-sequence doesn't exist How to find distinct rows with field in list using JPA and Spring? Spring Data JPA and Exists query

Examples related to validation

Rails 2.3.4 Persisting Model on Validation Failure Input type number "only numeric value" validation How can I manually set an Angular form field as invalid? Laravel Password & Password_Confirmation Validation Reactjs - Form input validation Get all validation errors from Angular 2 FormGroup Min / Max Validator in Angular 2 Final How to validate white spaces/empty spaces? [Angular 2] How to Validate on Max File Size in Laravel? WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jquery

Examples related to spring-mvc

Two Page Login with Spring Security 3.2.x ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean Spring 5.0.3 RequestRejectedException: The request was rejected because the URL was not normalized The type WebMvcConfigurerAdapter is deprecated RestClientException: Could not extract response. no suitable HttpMessageConverter found Spring boot: Unable to start embedded Tomcat servlet container UnsatisfiedDependencyException: Error creating bean with name 8080 port already taken issue when trying to redeploy project from Spring Tool Suite IDE Error creating bean with name 'entityManagerFactory' defined in class path resource : Invocation of init method failed Difference between the annotations @GetMapping and @RequestMapping(method = RequestMethod.GET)