Pagination example

While developing application, there comes a situation where we do not want to retrieve all the values at once. As per the user requirement we have to fetch may be first 10 rows at a time and the next rows for the next request and so on. This feature is called pagination and HQL helps us in achieving this feature.
After querying for all the records required, we have to set two properties in the query. They are setFirstResult and setMaxResults. In the below example we have setFirstResult to 3, which means that we are asking hibernate to skip the first 3 records in the table. And then we set the setMaxResults to 5. This means that after skipping the first 3 results, hibernate has to pull the next 5 recods in the database. The result of this is printed in the console.
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
        <property name="connection.username">hr</property>
        <property name="connection.password">hr</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.OracleDialect</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <!-- Names the annotated entity class -->
        <mapping class="com.ram.dao.Student"/>

    </session-factory>

</hibernate-configuration>
File: com.ram.dao.Student.java
package com.ram.dao;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Student {
 @Id @GeneratedValue
 private int studentId;
 private String studentName;
 
 public int getStudentId() {
  return studentId;
 }
 public void setStudentId(int studentId) {
  this.studentId = studentId;
 }
 public String getStudentName() {
  return studentName;
 }
 public void setStudentName(String studentName) {
  this.studentName = studentName;
 }
}

File: com.ram.hibernate.Pagination.java
package com.ram.hibernate;

import java.util.List;

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

import com.ram.dao.Student;

public class Pagination {
 public static void main(String[] args) {
  SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
  Session session = sessionFactory.openSession();
  session.beginTransaction();
  
  Query query = session.createQuery("from Student");
  query.setFirstResult(3);
  query.setMaxResults(5);
  
  List students = (List) query.list();
  for(Student stud: students){
   System.out.print("Student Id = "+stud.getStudentId());
   System.out.println("\tStudent Name = "+stud.getStudentName());
  }
  
  session.getTransaction().commit();
  session.close();
 }
}

Execute Pagination.java and you get the output as shown below.

No comments:

Post a Comment