Hibernate Framework Example 2

This article is in continuation from the previous article Hibernate Framework Example 1. We will drill down more into how we could validate our application in complex scenarios such as:

Imagine a class with three parameters a, b and c with one field called “a” whose value can be 0 or anything.

Case 1: If the value of this “a” is 0 then the other fields (b and c) in the class have to be in range 0 to 255 while

Case 2: If “a” is not 0 then value of b and c can be from 0 to 65535.

So in order to validate such a scenario, it becomes necessary to use xmls file one for a: 0 and other for a: not 0.

The below xml would validate b and c parameters in the scenario when “a” is 0 and validatorFields.xml would appear like:

<constraint-mappings xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd” xmlns=”http://jboss.org/xml/ns/javax/validation/mapping”&gt;

<default-package>com.suri.hibernate </default-package>

<bean class=” com.suri.hibernate.HibernateEventPojo ” ignore-annotations=”true”>

<field name=”b“>

<constraint annotation=”javax.validation.constraints.Min“>

<element name=”value”>0</element>

</constraint>

<constraint annotation=”javax.validation.constraints.Max“>

<element name=”value”>255</element>

</constraint>

</field>

<field name=”c“>

<constraint annotation=”javax.validation.constraints.Min“>

<element name=”value”>0</element>

</constraint>

<constraint annotation=”javax.validation.constraints.Max“>

<element name=”value”>255</element>

</constraint>

</field>

</bean>

 

The below xml would validate b and c parameters in the scenario when “a” is non 0 and validatorFieldsNonA.xml would appear like:

<constraint-mappings xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd” xmlns=”http://jboss.org/xml/ns/javax/validation/mapping”&gt;

<default-package>com.suri.hibernate </default-package>

<bean class=” com.suri.hibernate.HibernateEventPojo ” ignore-annotations=”true”>

<field name=”b“>

<constraint annotation=”javax.validation.constraints.Min“>

<element name=”value”>0</element>

</constraint>

<constraint annotation=”javax.validation.constraints.Max“>

<element name=”value”>65535</element>

</constraint>

</field>

<field name=”c“>

<constraint annotation=”javax.validation.constraints.Min“>

<element name=”value”>0</element>

</constraint>

<constraint annotation=”javax.validation.constraints.Max“>

<element name=”value”>65535</element>

</constraint>

</field>

</bean>

The above xml’s would be used based on the value of parameter “a” and would be called like:

InputStream inputStream;

if (apiPojo.getA() == 0) {

// where apiPojo is instance of the class HibernateEventPojo defined in blog: Hibernate //Framework Example 1

inputStream = getClass().getResourceAsStream(“/validatorFields.xml“);

} else {

inputStream = getClass().getResourceAsStream(“/validatorFieldsNonA.xml“);

}

// this input stream read is added to the validator instance while building it.

validator = configuration

.addMapping(inputStream)

.buildValidatorFactory()

.getValidator();

Set<ConstraintViolation<?>> violationMap = new HashSet<ConstraintViolation<?>>();

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

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