Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Hibernate Validation

In this tutorial we will describe how to validate you application in hibernate.

Ads

Tutorials   
When should you use Hibernate? What's new in Hibernate 5? How much time it takes to learn Hibernate? Hibernate envers custom revinfo table Hibernate 5 Envers Hibernate Envers get all revisions Hibernate Examples Hibernate 5 build SessionFactory Example Hibernate 5 SessionFactory Example Hibernate 5 Annotation Example How to download Hibernate 5.x? Hibernate 5 Features Hibernate 5 Tutorials Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORM with Java How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate ORM 4.2.21.Final Released - Create CURD application in latest Hibernate Hibernate ORM 4.2.21 Tutorial - Learn Hibernate ORM Framework Hibernate Error: Failed to create sessionFactory object.java.lang.NoClassDefFoundError How to create Hibernate Project in Eclipse? Examples of Hibernate Criteria Query Hibernate ORM 4.3.6.Final released Hibernate ORM 4.2.14.Final released Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate association and join example Learn Hibernate programming with Examples Introduction to Hibernate Framework Hibernate Tutorial: Learn Hibernate with examples Hibernate 4.3 Hello World: Example Hibernate ORM 4.3.1.Final released How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate ORM 4.3.0.Final released Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials
Ads

Hibernate Validation

In this tutorial we will describe how to validate you application in hibernate.

Hibernate Validation  : Validation is the concept to check whether your data is valid or not. Data validation is very common and important process for any application.
Validation is required in presentation layer as well as persistent layer. It is wastage of time to apply validation in each layer.
Hibernate provides very strong annotations validation source. It works directly with your domain model. It reduces overhead of applying validation in each layer.

You can use constraint its java annotation for validation. Now we are going to describe how to annotate an object with these annotation.

Example:  In this example we are using most of validation constraint to validate name, roll, email.

@NotEmpty- checks the element not be empty. It is defined in org.hibernate.validator.constraints.NotEmpty class .

@NotNull - Checks the element not null. It is defined in javax.validation.constraints.NotNull class.

@Max(N) - checks the max length not exceed to 'N'. It is defined in javax.validation.constraints.Max class.

@Min(N) -checks the length not less than 'N'.  It is defined in javax.validation.constraints.Min class.

@Email- Checks the inputted element is in valid email format.  It is defined in javax.validation.constraints.Email class.

package net.roseindia.student;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;

public class Student {

@NotEmpty(message = "Please Specify Name")
private String name;

@NotNull
@Max(110)
@Min(1)
private Integer roll;
private String gender;
private String country;
private String community;

@Email(message = "Enter valid email id")
private String email;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getRoll() {
return roll;
}

public void setRoll(Integer roll) {
this.roll = roll;
}

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public String getCommunity() {
return community;
}

public void setCommunity(String community) {
this.community = community;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

}

Output:

Click here to download complete source code

Advertisements

Online Training Classes

Share on Google+Share on Google+

Hibernate Validation

Posted on: July 13, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Hibernate Validation  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Jihed
January 29, 2014
project dependencies

Please let me know about this project dependencies Think you

Ads

 

Ads