Validation Example in Hibernate

In this article, we will cover How to validate fields in Hibernate using Java bean Validator.

Validation is an important part of any web application. It helps us to create error-free code or applications. It is required from the security point of view.

  • Nowadays, we have a good amount of libraries available for Validation.
  • Whether it is client-side or Server-side each Framework is building itself to provide Validation Libraries to the developers.
  • We just need to add it and use it.

Hibernate Validator provides us with classes to validate Java beans. It has a good amount of annotation that we can apply to the property of the bean. Let’s see some of them with descriptions.



@Email It validates whether the character sequence is a valid email.
@Max It validates whether the value is less than or equal to the specified maximum.
@Min It validates whether the value is greater than or equal to the specified minimum.
@NotEmpty It validates whether the value is not null/not empty.
@Null It validates whether the value is null.
@NotNull It validates whether the value is not null.
@NotBlank It validates whether the value is not null and trimmed length > 0
@Digits It validates whether the value is a number.
@Pattern It validates whether the value matches with regex.
@Size It validates whether the value is between the min and max.
@Negative It validates whether the value is Negative
@NegativeorZero It is the same as @Neagtive but allows Zero value.
@Future It validates whether the date is of future
@FutureOrPresent It validates whether the date is in the present or in the future.
@PastOrPresent It validates whether the date is in the past or in the present.

Let’s see how we can implement it.

Create a Maven project and add the following dependencies in pom.xml

  • Add the Java bean Validation API.
  • Add the Hibernate Validator
  • Add the Hibernate validator annotation processor to verify validation annotations usage at compile time.
  • Add the javax-el-api and add the implementation java.ex


<project xmlns="" xmlns:xsi=""




    <!-- Java bean validation API - Spec -->

    <!-- Hibernate validator - Bean validation API Implementation -->

    <!-- Verify validation annotations usage at compile time -->

    <!-- Unified Expression Language - Spec -->

    <!-- Unified Expression Language - Implementation -->

Create a Bean Class with annotation.

package com.Hibernate_Validation;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class Student {

  @NotNull(message = "Please Enter Id")
  private long id;
  @Size(max = 20, min = 5, message = "{}")
  @NotEmpty(message = "Please enter name")
  private String name;
  @Email(message = "{}")
  @NotEmpty(message = "Please enter email")
  private String email;

  public Student(@NotNull(message = "Please Enter Id") long id,
      @Size(max = 20, min = 5, message = "{}") @NotEmpty(message = "Please enter name") String name,
      @Email(message = "{}") @NotEmpty(message = "Please enter email") String email) {
    super(); = id; = name; = email;

  public long getId() {
    return id;

  public void setId(long id) { = id;

  public String getName() {
    return name;

  public void setName(String name) { = name;

  public String getEmail() {
    return email;

  public void setEmail(String email) { = email;

Create a property file to store the message in Key value pair. (Just right-click on the project name and click New > Other > Search properties > Ok) Name Email Id.

Now, Test the Validation in

package com.Hibernate_Validation;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import org.hibernate.validator.ap.ConstraintValidationProcessor;

 * Hello world!
public class Test 
    public static void main( String[] args )
        ValidatorFactory validatorFactory =Validation.buildDefaultValidatorFactory();
        Validator validator =validatorFactory.getValidator();
        Student student = new Student(1, "N", "n");
        Set<ConstraintViolation<Student>> constraintViolations = validator.validate(student);
        	for(ConstraintViolation<Student> constraintViolation2 :  constraintViolations ){
  • On line no: 20, Create a ValidatorFactory that will return the validator.
  •  On line no: 21, It validates the instance of a bean.
  • On line no: 22, Student Bean Object is created and passed with invalid Name and invalid Email id and valid Id.
  • On line no: 23, ConstarintViolation Interface is used to describe constraint violation.
  • On line no: 24, We checked the constraint and display errors if any.


May 04, 2021 10:19:59 AM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 6.0.11.Final
Invalid Name
Invalid Email Id.

Here, we can see the Message Invalid Name and Invalid Email from the property File. Thus, this is how we do validation in Hibernate using Java Bean Validator.