What is Criteria api?
Criteria api in Hibernate is used to fetch data from database tables. Though Hibernate Query Language can also be used to perform the same task but using queries makes maintenance difficult since the queries need to be written in string format while using Criteria involves classes and their properties. It should be noted that Criteria can only be used to fetch data(or SELECT operations) and not for data manipulation tasks such as Insert, Update or Delete.
With Criteria api, you can
1. Fetch all records from a table(all columns and rows).
2. Fetch only selected columns.
3. Fetch records matching some condition(such as a website with name ‘codippa’)
4. Apply sorting on the records while fetching.
5. Apply JOINS for fetching records from multiple tables.
This post will guide you through the method of creating a criteria and use it to fetch records.
Database table and Entity
Before proceeding with learning about criteria, let us define the database table and the corresponding entity class. Following will be the structure of database table and sample records that will be referred throughout the examples. 

id name age eid salary
1 abc 22 e001 20000
2 def 25 e002 30000
3 ghi 27 e003 40000

Entity class corresponding to the above database table is given below

package com;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;


@Entity
@Table(name = "employee")
public class Student implements Serializable {
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name = "id")
	private int id;

	@Column(name = "name")
	private String empName;

	@Column(name = "age")
	private int age;

	@Column(name="eid")
	private String employeeId;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getEmpName() {
		return empName;
	}

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

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
      
        public String getEmployeeId() {
                return employeeId;
        }
        public void setEmployeeId(String eid) { 
                this.employeeId = eid; 
       }

}

Using Criteria api
To start using Criteria api, the first step is getting a criteria object which belongs to org.hibernate package. This object will be used to leverage the features of Criteria classes. An org.hibernate.Criteria object is retrieved by using createCriteria method on a org.hibernate.Session object. This method takes the name of the class(or entity) on which you want the criteria methods to be applied. Following sections will clarify the various uses that a criteria object can be put to.

org.hibernate.Criteria is an interface. When its object is created, it is of type org.hibernae.internal.CriteriaImpl class

Fetching all records
A criteria object can be used to fetch all records from a table. By all records, we mean complete rows and columns of the database table corresponding to the entity for which the criteria object is created. Example,