Java Libraries
Copy JAR files which are listed below:
antlr-2.7.7.jar
classmate-1.3.4.jar
commons-lang3-3.6.jar
commons-logging-1.1.3.jar
dom4j-1.6.1.jar
ehcache-core-2.6.11.jar
geolatte-geom-1.1.0.jar
hibernate-commons-annotations-5.0.1.Final.jar
hibernate-core-5.2.11.Final.jar
hibernate-ehcache-5.2.12.Final.jar
hibernate-ejb3-persistence.jar
hibernate-enhance-maven-plugin-4.3.7.Final.jar
hibernate-entitymanager-5.2.12.Final.jar
hibernate-java8-5.2.11.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
hibernate-spatial-5.2.11.Final.jar
hibernate-validator-6.0.2.Final.jar
javassist-3.16.1-GA.jar
jboss-logging-3.3.1.Final.jar
jboss-transaction-api_1.1_spec-1.0.1.Final.jar
jts-1.11.jar
mysql-connector-java-5.1.36.jar
slf4j-api-1.7.21.jar
classmate-1.3.0.jar
el-api-2.2.1-b04.jar
hibernate-validator-5.2.2.Final.jar
validation-api-1.1.0.Final.jar
Entities Class
Create a entity classes – Form.java put annotations to validate values of form object. Create 2 groups: Group1 and Group2 with interfaces as below
Form.java
package entities;
import org.hibernate.validator.constraints.NotEmpty;
public class Form {
@NotEmpty(groups = { Group1.class })
private String field1;
@NotEmpty(groups = { Group2.class })
private String field2;
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}
public interface Group1 {
};
public interface Group2 {
};
}
Run It
package validator;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import entities.Form;
public class Main {
public static void main(String[] args) {
try {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
System.out.println("Validate Form 1 with Group 1");
Form form1 = new Form();
form1.setField1("");
Set<ConstraintViolation<Form>> constraintViolationsForm1 = validator.validate(form1, Form.Group1.class);
if (!constraintViolationsForm1.isEmpty()) {
for (ConstraintViolation<Form> constraintViolation : constraintViolationsForm1) {
System.out.println(constraintViolation.getPropertyPath() + " " + constraintViolation.getMessage());
}
} else {
System.out.println("Form 1 is valid with Group 1");
}
System.out.println("\nValidate Form 2 with Group 2");
Form form2 = new Form();
form2.setField2("");
Set<ConstraintViolation<Form>> constraintViolationsForm2 = validator.validate(form2, Form.Group2.class);
if (!constraintViolationsForm2.isEmpty()) {
for (ConstraintViolation<Form> constraintViolation : constraintViolationsForm2) {
System.out.println(constraintViolation.getPropertyPath() + " " + constraintViolation.getMessage());
}
} else {
System.out.println("Form 2 is valid with Group 2");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Output
Validate Form 1 with Group 1
field1 may not be empty
Validate Form 2 with Group 2
field2 may not be empty