<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div style="color: #20124d;">In this tutorial of ;Pagination in Hibernate Query Language (HQL) we will discuss about the pagination query in hibernate for pulling specific number data with different pages. Pagination of search results is a common requirement for any application.</p>
<div align='center' id="ads-id"></div>
</div>
<div style="color: #20124d;">Out of performance reasons it is recommended to restrict the number of returned objects per query. In fact is a very common use case anyway that the user navigates from one page to an other. The way to define pagination is exactly the way you would define pagination in a plain HQL or Criteria query.</div>
<div style="color: #20124d;">
Using the <b>createQuery()</b> method of a <b>Session</b> object that returns a <b>Query</b> object. First, instantiate the Session object using the openSession() method of <b>SessionFactory</b>. Then, invoke the <i>createQuery()</i> method on the resulting object.</div>
<div style="color: #20124d;"></p>
<pre class="highlight">Query q = session.createQuery("...");
q.setFirstResult(start);
q.setMaxResults(length);</pre>
</div>
</div>
<p><b><br />
</b> <b>Student.java</b></p>
<pre class="highlight" name="code">package com.sdnext.hibernate.tutorial.dto;

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.Table;

@Entity
@Table(name="STUDENT")
public class Student implements Serializable 
{
 /**
 * serialVersionUID
 */
 private static final long serialVersionUID = 8633415090390966715L;
 @Id
 @Column(name="ID")
 @GeneratedValue(strategy=GenerationType.AUTO)
 private int id;
 @Column(name="STUDENT_NAME")
 private String studentName;
 @Column(name="ROLL_NUMBER")
 private int rollNumber;
 @Column(name="COURSE")
 private String course;
 public int getId() {
 return id;
 }
 public void setId(int id) {
 this.id = id;
 }
 public String getStudentName() {
 return studentName;
 }
 public void setStudentName(String studentName) {
 this.studentName = studentName;
 }
 public int getRollNumber() {
 return rollNumber;
 }
 public void setRollNumber(int rollNumber) {
 this.rollNumber = rollNumber;
 }
 public String getCourse() {
 return course;
 }
 public void setCourse(String course) {
 this.course = course;
 }
 public String toString()
 {
 return "ROLL Number: "+rollNumber+"| Name: "+studentName+"| Course: "+course;
 }
}
</pre>
<p>
</div>
<p><b><br />
</b> <b>hibernate.cfg.xml</b><br />
<b><br />
</b> </p>
<pre class="highlight" name="code"><;hibernate-configuration>; 
 <;session-factory>; 
 <;!-- Database connection settings -->;
 <;property name="connection.driver_class">;com.mysql.jdbc.Driver<;/property>; 
 <;property name="connection.url">;jdbc:mysql://localhost:3306/hibernateDB2<;/property>; 
 <;property name="connection.username">;root<;/property>; 
 <;property name="connection.password">;root<;/property>; 

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

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

 <;!-- Enable Hibernate's automatic session context management -->;
 <;property name="current_session_context_class">;thread<;/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">;update<;/property>; 
 
 
 <;mapping class="com.sdnext.hibernate.tutorial.dto.Student">;
 
 <;/mapping>;<;/session-factory>; 
 <;/hibernate-configuration>;
</pre>
<p>
<b>HibernateTestDemo.java</b></p>
<pre class="highlight" name="code">package com.sdnext.hibernate.tutorial;

import java.util.List;

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

import com.sdnext.hibernate.tutorial.dto.Student;

public class HibernateTestDemo {
 /**
 * @param args
 */
 public static void main(String[] args) 
 {
 SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
 Session session = sessionFactory.openSession();
 session.beginTransaction();
 
 String SQL_QUERY = "FROM Student student";
 Query query = session.createQuery(SQL_QUERY);
 query.setFirstResult(1);//set first result start value
 query.setMaxResults(5);//number of result to be display
 
 List students = query.list();
 for(Student student : students)
 {
 System.out.println(student);
 }
 session.getTransaction().commit();
 session.close();
 }
}
</pre>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">
<div style="color: blue;"><b>Output:</b></div>
<div style="color: red;">log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).</div>
<div style="color: red;">log4j:WARN Please initialize the log4j system properly.</div>
<p>Hibernate: select student0_.ID as ID0_, student0_.COURSE as COURSE0_, student0_.ROLL_NUMBER as ROLL3_0_, student0_.STUDENT_NAME as STUDENT4_0_ from STUDENT student0_ limit ?, ?<br />
ROLL Number: 2| Name: Sweety Rajput| Course: PGDCP<br />
ROLL Number: 3| Name: Adesh Rajput| Course: MA<br />
ROLL Number: 4| Name: DEV| Course: MA<br />
ROLL Number: 5| Name: RAJ| Course: BA<br />
ROLL Number: 6| Name: Pradeep| Course: BA</div>
<p></p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" height="132" src="http://50.87.249.200/~dineshon/wp-content/uploads/2017/04/pagination.jpg" width="640"/></div>
</div>
<p><b>In the <a href="https://dineshonjava.com/p/understanding-parameter-binding-and-sql.html">Next Chapter</a> we will discuss about the <a href="https://dineshonjava.com/p/understanding-parameter-binding-and-sql.html">Parameter Binding and SQL Injection</a>.</b><br />
<b><br />
</b> <b> ;  ;  ;  ;  ;  ;  ; ;</b><br />
<b> ;  ;  ;  ;  ; ;<;<;<a href="https://dineshonjava.com/p/introducing-hqlhibernate-query-language.html" style="color: blue; font-family: Times, 'Times New Roman', serif; font-size: x-large; line-height: 14px;">Previous Chapter 26</a><;<;  ; ; >;>;N<a href="https://dineshonjava.com/p/understanding-parameter-binding-and-sql.html" style="color: blue; font-size: x-large; line-height: 14px;">ext Chapter 28</a>>;>;</b><br />
<b><br />
</b> <b><br />
</b> <b><br />
</b> <b><br />
</b> <b><br />
</b></div>