Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




HQL - Pagination

Say you are dealing with a web application and you are asked to show the data in pages. In that case Hibernate Pagination comes handy.

It can be achieved using the below methods:

  1. setFirstResult(int value) :
It defines the row number from which the value has to be fetched. It starts from '0'.

  1. setMaxResults(int value) :
It fetches the number of rows mentioned in 'value'.

We will see in detail, how on combining the above two methods, pagination can be achieved.

Let us take the Employee class :

Employee

class Employee {

   int id;
   String name;

   -- getters and setters --
}

There should be a simple mapping file for the same :



Employee.hbm.xml

<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class name = "Employee" table = "EMPLOYEE">
   <id name = "id" type = "string">
     <column name = "ID">
    </id>
    <property name = "name" type = "string">
     <column name = "NAME">
    </property>
   </class>
</hibernate-mapping>

Now, let us write the main() class and save three 'Employee' objects to database.


import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateSave {
   public static void main(String[] args) {

    static SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();

    Employee employeeJoe = new Employee();
    employee.setId(1);
    employee.setName("Joe");

    Employee employeePeter = new Employee();
    employee.setId(2);
    employee.setName("Peter");

    session.save(employeePeter);

    Employee employeeChang = new Employee();
    employee.setId(3);
    employee.setName("Chang");

    session.save(employeeChang);

    session.getTransaction().commit();

    session.close();
    sessionFactory.close();
 }
}

EMPLOYEE

ID NAME
1 Joe
2 Peter
3 Chang

How Pagination happens?

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateSave {
   public static void main(String[] args) {

    static SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();

    Query query = session.createQuery("from Employee");
    query.setFirstResult(1);
    query.setMaxResults(2);

    session.getTransaction().commit();

    session.close();
    sessionFactory.close();
 }
}

The below line tells Hibernate to fetch data from the second row(Since the count starts from '0').

query.setFirstResult(1);

And the next line tells Hibernate to fetch two rows from the second position.

query.setMaxResults(2);

And finally we get the below result.


ID NAME
2 Peter
3 Chang