Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Home Hibernate Hibernate4 Hibernate Validator Annotations

Related Tutorials


Share on Google+Share on Google+

Hibernate Validator Annotations

In this section, you will learn how to validate in Hibernate using validator annotations in bean class.

Hibernate Validator Annotations

In this section, you will learn how to validate in Hibernate using validator annotations in bean class.

Given below a annotated bean class named as :

package net.roseindia;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class User {
	private Long id;
	private String name;
	private String password;
	private String gender;
	private String country;
	private String aboutYou;
	private Boolean mailingList;
	@Column(name = "USER_ID")
	public Long getId() {
	return id;
	public void setId(Long id) { = id;
	@Size(min = 1, max = 50, message = "username must between 1 to 50 characters")
	@Column(name = "USER_NAME", nullable = false, length = 50)
	public String getName() {
		return name;
	public void setName(String name) { = name;
	@Size(min = 6, max = 10, message = "password must between 6 to 10 characters")
	@Column(name = "USER_PASSWORD", nullable = false, length = 10)
	public String getPassword() {
		return password;
	public void setPassword(String password) {
		this.password = password;
	@NotNull(message = "Please select a gender")
	@Column(name = "USER_GENDER")
	public String getGender() {
		return gender;
	public void setGender(String gender) {
		this.gender = gender;
	@NotNull(message = "Please select a country")
	@Column(name = "USER_COUNTRY")
	public String getCountry() {
		return country;
	public void setCountry(String country) { = country;
	@Size(max = 100)
	@Column(name = "USER_ABOUT_YOU", length = 100)
	public String getAboutYou() {
		return aboutYou;
	public void setAboutYou(String aboutYou) {
		this.aboutYou = aboutYou;
	@Column(name = "USER_MAILING_LIST")
	public Boolean getMailingList() {
		return mailingList;
	public void setMailingList(Boolean mailingList) {
		this.mailingList = mailingList;

Description of the annotations used above for validation is given below :

@NotNull : The element which is annotated using @NotNull  must not be null.

@Size: The element which is annotated using @Size must be within the specified limits using min and max.

For detail list of the Validator Annotation click here.


If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 12, 2012

Related Tutorials

Discuss: Hibernate Validator Annotations  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image