Hibernate ScrollResults & ScrollMode

ScrollableResults is used to scroll large amount of data and ScrollMode provides different modes such as scrolling only in forward direction and sensitive/insensitive to changes in underlying data.

ScrollableResults can be used to paginate large amount of data because it provides methods to change current location for row iteration. Here I will provide ScrollableResults examples using createCriteria(), createQuery() and createSQLQuery() of org.hibernate.Session.

  • org.hibernate.ScrollableResults : Scrolls results by the given increments. The columns of the results start from zero. Find some methods of ScrollableResults.

  • scroll(int i) : Scrolls the results. i represents the number for forward or backward scrolling.

  • setRowNumber(int rowNumber) : It sets the current location in ScrollableResults.

  • get(int i) : Gets the object at index i in current row.

  • get() : It returns the array of Object of current rows

  • afterLast() : It sets the location just after the last result.

  • beforeFirst() : It sets the location just before the first result.

  • org.hibernate.ScrollMode is an Enum which provides the different mode to fetch ScrollableResults.

  • FORWARD_ONLY : It requests ScrollableResults that scrolls forward only.

  • SCROLL_SENSITIVE : ScrollableResults that is sensitive to changes in underlying data.

  • SCROLL_INSENSITIVE : Insensitive to changes in underlying data.

Data used in Example :

Example Table Data

Jbkstudent.java

package com.javabykiran.hibernate;
// Generated Feb 24, 2020 2:00:10 PM by Hibernate Tools 5.2.12.Final

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
 * Jbkstudent generated by hbm2java
 */
@Entity
@Table(name = "jbkstudent")
public class Jbkstudent implements java.io.Serializable {

	private int sid;
	private String sname;

	public Jbkstudent() {
	}

	public Jbkstudent(int sid) {
            this.sid = sid;
	}

	public Jbkstudent(int sid, String sname) {
            this.sid = sid;
            this.sname = sname;
	}

	@Id
	@Column(name = "sid", unique = true, nullable = false)
	public int getSid() {
            return this.sid;
	}

	public void setSid(int sid) {
            this.sid = sid;
	}

	@Column(name = "sname", length = 45)
	public String getSname() {
            return this.sname;
	}

	public void setSname(String sname) {
            this.sname = sname;
	}
}

Hibernate.cfg.xml

<?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>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jbk_hibernate</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
    </session-factory>
</hibernate-configuration>

  • Scroll Results with Session.createCriteria()

JbkScrollableResultsWithCriteria.java

package com.javabykiran.hibernate;

import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

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

        Configuration cfg = new Configuration();
        cfg.configure().addAnnotatedClass(Jbkstudent.class);

	SessionFactory sf = cfg.buildSessionFactory();
	Session session = sf.openSession();

	ScrollableResults sr = session.createCriteria(Jbkstudent.class).scroll(ScrollMode.FORWARD_ONLY);
        while (sr.next()) {
            Jbkstudent stu = (Jbkstudent) sr.get(0);
            System.out.println(stu.getSid() + " " + stu.getSname());
	}
	System.out.println("After Setting location");

	sr.setRowNumber(1);
	while (sr.next()) {
            Jbkstudent js = (Jbkstudent) sr.get(0);
            System.out.println(js.getSid() + " " + js.getSname());
	}
    }
}

  • Scroll Results with Session.createQuery()

JbkScrollableResultsWithQuery.java

package com.javabykiran.hibernate;

import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class JbkScrollableResultsWithQuery {
    public static void main(String[] args) {
        Configuration cfg = new Configuration();
        cfg.configure().addAnnotatedClass(Jbkstudent.class);
        SessionFactory sf = cfg.buildSessionFactory();
        Session session = sf.openSession();
        ScrollableResults sr = session.createQuery("from Jbkstudent").scroll(ScrollMode.SCROLL_INSENSITIVE);

        while (sr.next()) {
            Jbkstudent stu = (Jbkstudent) sr.get(0);
            System.out.println(stu.getSid() + " " + stu.getSname());
            sr.afterLast();
        }
    }
}

  • Scroll Results with Session.createSQLQuery()

JbkScrollableResultsWithSQLQuery.java

package com.javabykiran.hibernate;

import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class JbkScrollableResultsWithSQLQuery {
    public static void main(String[] args) {
	Configuration cfg = new Configuration();
	cfg.configure().addAnnotatedClass(Jbkstudent.class);

	SessionFactory sf = cfg.buildSessionFactory();
	Session session = sf.openSession();
        ScrollableResults sr = session.createSQLQuery("select sid,sname from Jbkstudent")
			       .scroll(ScrollMode.SCROLL_SENSITIVE);

	while (sr.next()) {
            Object row[] = sr.get();
            System.out.println(row[0] + " " + row[1]);
	}
	session.close();
    }
}