JPA Bean Validation

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

This article demonstrates how  constraints can be used along with jpa bean validation and how constraints can be retrieved when doing jpa bean validation.

JPA Bean Validation:

JPA bean validation isn’t directly related to JPA itself. Package javax.validation.constraints provides a way to validate a java bean using constraints which we can use for jpa bean validation. When constraints are applied to a bean, validator needs to be invoked. It can be done in following ways.

In Container Environment:

JPA bean validation can be done in java EE environment using container managed validator. It is retrieved using @Resource annotation as shown below.

In JavaSE Environment:

Validator can be retrieved in a simple javaSE environment using code below.

JPA Bean Validation Example:

Now that we know how to retrieve a Validator instance, let us see a jpa bean validation example. We have an employee entity as below.

Note that @NotNull means null value is not allowed for firstName, lastName and salary and @DecimalMax means salary cannot be greater than 50000.

Let us try jpa bean validation now.

This produces output as below

salary  must be less than or equal to 50000.00
lastName  may not be null

Let us see what each line does:

  • Validation.buildDefaultValidatorFactory(); creates validator factory from default implementation available that you have provided in a jar file.
  • Validator validator = factory.getValidator();  gets validator instance from factory.
  • validator.validate(employee);  performs validation on employee entity and returns a Set of ConstraintViolation.
  • constraint.getPropertyPath();  and constraint.getMessage(); return the name of property for which validation failed and corresponding message.

I hope this jpa bean validation example helped understand how beans can be validated.


References for further study :



Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

7 thoughts on “JPA Bean Validation

  • Pingback:creating custom constraints | theJavaGeek

  • March 27, 2015 at 6:21 pm

    I’m from a PHP background, new to Java EE stack. Struggled a lot to clear up how to get some of the things done with Java. Your short & sweet article really helped me to learn hot to validate a JPA entity at JavaSE environment. Thank you Prasad.

  • August 17, 2017 at 6:44 pm

    Hello good time.
    I’ve created a new project with netbeans and using beansBinding. Where JPA, EclipseLink is used.
    I’m having trouble with Validation. When using your example validation code, it works in a Hibernate project, but in my project with EclipseLink, the following error occurs:

    Exception in thread “main” javax.validation.ValidationException: Unable to find a default provider
    at javax.validation.Validation$GenericBootstrapImpl.configure(
    at javax.validation.Validation.buildDefaultValidatorFactory(
    at SampleDB.NewClass.main(
    C:\Users\Pc\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
    BUILD FAILED (total time: 0 seconds)

    Please tell me what kind of libraries are needed, such as “validation-api-1.0.0.GA”.

    Please respond to my email. thank you.

    • August 19, 2017 at 10:14 pm

      working in hibernate project because hibernate is a provider and it has the library. Most certainly the eclipselink library is not included.

  • July 27, 2018 at 1:14 pm

    i have a question. how can i generate
    @Size(max = 25)
    private String firstName;

    @Size(max = 25)
    private String lastName;
    annotations @Size form the table(i don’t want write itself)


Leave a Reply

Your email address will not be published. Required fields are marked *