Hibernate Framework Example 1

The above featured image rightly explains one of the areas where Hibernate could be used! Yes, you guessed it right in Validation of fields.

Hibernate is a Java Persistence Framework which is an Object Relational Mapping technique where Hibernate would be between Database and the Java Object.

Hibernate Advantage:

a) Provides a technique to validate application constraints.

b) It takes care of the mapping between Java classes and database tables using the XML files. This is advantageous in the sense that if database has to be changed then only the change has to be done in the xml file instead of any change in database directly.

c) Provides simple API for storing and retrieving Java objects directly to and from the database.

In this article, we will focus on validating application constraints using Hibernate Framework:-

Validation of Application Constraints

  1. Using the default annotations: @Range(min = , max = ) ; @Length(min = , max = ); @NotNull, @Min(), @Size(min = , max = )
  2. Using customized Validator: for a field or for a class

 

Example 1: Imagine a class with fields that are subject to following constraints

a) fields can not be null

b) sum of fields can not be less than 30

c) each field has to be between 0 and 21

Solution:

a. The not null field validation could be done easily through the annotation @NotNull

b. In order to validate the sum of all the fields to be more than 30, class level validation is to be done.

c. Each field – specific range can be achieved through @Range annotation.

@SumValidator

public class HibernateEventPojo {

@NotNull

@Range(min = 0, max = 21)

private int a;

@NotNull

@Range(min = 0, max = 21)

private int b;

@NotNull

@Range(min = 0, max = 21)

private int c;

public int getA() {

return a;

}

public void setA(int a) {

this.a = a;

}

public int getB() {

return b;

}

public void setB(int b) {

this.b = b;

}

public int getC() {

return c;

}

public void setC(int c) {

this.c = c;

}

}

For the class level validation in terms of validating the sum to be more than 30, the following customised Validator has to be prepared.

@Documented

@Retention(RetentionPolicy.RUNTIME)

// Class Level Validator

@Target({ElementType.TYPE, ElementType.ANNOTATION_TYPE})

@Constraint(validatedBy = SumOfParametersValidatorImpl.class)

public @interface SumValidator {

String DEFAULT_MESSAGE = “Sum is out of Range”;

String message() default DEFAULT_MESSAGE;

//   an attribute groups that allows the specification of validation groups, to which this

//  constraint belongs. This must default to an empty array of type Class<?>.

   Class<?>[] groups() default {};

   /*   an attribute payload can be used to assign custom payload objects to a constraint. It can be for instance used to  assign the severity to an instance and after the validation if there is a constraint violation then we can access the severity of a constraint using ConstraintViolation.getConstraintDescriptor().getPayload() and adjust its behaviour depending on the severity.

*/

Class<? extends Payload>[] payload() default {};

}

public class SumOfParametersValidatorImpl implements ConstraintValidator< SumOfParametersValidator, HibernateEventPojo> {

@Override

public void initialize(SumOfParametersValidator sumOfParametersValidator) {}

@Override

public boolean isValid(EventPojo apiPojo, ConstraintValidatorContext context) {

if (apiPojo == null) {

return true;

}

Boolean flag = true;

if( 30 >  (apiPojo.getA() + apiPojo.getB() + apiPojo.getC())) {

// enable the reading of the customized message.

context.disableDefaultConstraintViolation();

context.buildConstraintViolationWithTemplate(SumOfParametersValidator.DEFAULT_ MESSAGE).addConstraintViolation();

flag = false;

}

return flag;

}

}

This constraint violation can then be fetched from the class where validator instance would have been created to validate this simple Pojo using the statement like:

Set<ConstraintViolation<EventPojo >> constraintViolationSet = validator.validate(apiPojo);

where apiPojo is the object of HibernateEventPojo class.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s