Hibernate Criteria Query API / HQL Example

Hibernate Criteria Example

Criteria APIs in the hibernate framework is useful for creating the dynamic query to execute.It is an alternative way to write the queries without using HQL. The queries are generated at runtime and excuted on the fly. Application developer need not worry about writing the query in hand, he/she just need to use APIs provided in the hibernate. It is one of the elegant way to write the queries in hibernate.

also read:

The following example demonstrates very simple hibernate criteria example with retriving 10 results from the table. This is the statement used for creating the Criteria object :

	Criteria criteria = session.createCriteria(Student.class)

The above code returns the Criteria for Student class. Once Criteria is created, we can access methods in the API
to set the conditions. In our exampl we called


It sets the conditionto retrieve only 10 results.

This is very basic example for the beginner, in the coming sections we will explain how to create the more
complex queries using the Hibernate Criteria API.


package javabeat.net.hibernate;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

 * source : www.javabeat.net
public class JavaBeatHibernateExample {

    public static void main(String args[]) {
        Configuration configuration = new Configuration();
        // configuring hibernate
        SessionFactory sessionFactory = configuration.configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Criteria criteria = session.createCriteria(Student.class);
        List<student> list = criteria.list();
        for (Student  student:list)


package javabeat.net.hibernate;

 * source : www.javabeat.net
public class Student {
    private long id;
    private String  name;
    private String dept;
    private int marks;
    private String section;

    public String getDept() {
        return dept;

    public void setDept(String dept) {
        this.dept = dept;

    public long getId() {
        return id;

    public void setId(long id) {
        this.id = id;

    public int getMarks() {
        return marks;

    public void setMarks(int marks) {
        this.marks = marks;

    public String getName() {
        return name;

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

    public String getSection() {
        return section;

    public void setSection(String section) {
        this.section = section;


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
    <class name="javabeat.net.hibernate.Student" table="Student_Details">
        <id name="id" column="id" type="java.lang.Long">
            <generator class="increment"/>
        <property name="name" column="name" type="java.lang.String"/>
        <property name="dept" column="dept" type="java.lang.String"/>
        <property name="marks" column="marks" type="java.lang.Integer"/>
        <property name="section" column="section" type="java.lang.String"/>



About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

  • ram

    very goood

  • Ankur

    Awesome . tuto..!! superlike..!!

  • ankit srivastava

    good explanation

  • naresh bhabat

    nice examples