Hibernate ScrollableResults and ScrollMode Example

By Arvind Rai, November 29, 2015
On this page, we will provide Hibernate ScrollableResults and ScrollMode example. 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.

ScrollableResults

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 row.s
afterLast() : It sets the location just after the last result.
beforeFirst() : It sets the location just before the first result.

ScrollMode

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

Hibernate ScrollableResults and ScrollMode Example

build.gradle

apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'hibernate-4'
version = '1' 
repositories {
    mavenCentral()
}
dependencies {
    compile 'org.hibernate:hibernate-core:4.3.8.Final'
    compile 'mysql:mysql-connector-java:5.1.34'
}  

Hibernate Utility and Entity Class


HibernateUtil.java
package com.concretepage;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
    private static SessionFactory sessionFactory ;
    static {
       Configuration configuration = new Configuration().configure();
       StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()
	     .applySettings(configuration.getProperties());
       sessionFactory = configuration.buildSessionFactory(builder.build());
    }
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}  

Student.java
package com.concretepage;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="student")
public class Student {
	@Id
	@Column(name="id")
	private int id;
	@Column(name="name")
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
} 

Scroll Results with Session.createCriteria()


ScrollableResultsWithCriteria.java
package com.concretepage;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
public class ScrollableResultsWithCriteria {
	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		ScrollableResults scResults = session.createCriteria(Student.class)
		             .scroll(ScrollMode.FORWARD_ONLY);
		while(scResults.next()) {
			Student s = (Student)scResults.get(0);
			System.out.println(s.getId()+", "+s.getName());
		}
		//Set the location at index 2
		System.out.println("After setting location");
		scResults.setRowNumber(1);
		while(scResults.next()) {
			Student s = (Student)scResults.get(0);
			System.out.println(s.getId()+", "+s.getName());
		}
		session.close();
	}
} 
Find the output.
Hibernate: select this_.id as id1_0_0_, this_.name as name2_0_0_ from student this_
1, AA
2, BB
3, CC
After setting location
3, CC 

Scroll Results with Session.createQuery()


ScrollableResultsWithQuery.java
package com.concretepage;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
public class ScrollableResultsWithQuery {
	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		ScrollableResults scResults = session.createQuery("from Student")
		       .scroll(ScrollMode.SCROLL_INSENSITIVE);
		while(scResults.next()) {
			Student s = (Student)scResults.get(0);
			System.out.println(s.getId()+", "+s.getName());
			scResults.afterLast();
		}
		session.close();
	}
} 
Find the output.
Hibernate: select student0_.id as id1_0_, student0_.name as name2_0_ from student student0_
1, AA 

Scroll Results with Session.createSQLQuery()


ScrollableResultsWithSQLQuery.java
package com.concretepage;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
public class ScrollableResultsWithSQLQuery {
	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		ScrollableResults scResults = session.createSQLQuery("SELECT id, name FROM student")
				.scroll(ScrollMode.SCROLL_SENSITIVE);
		while(scResults.next()) {
			Object[] row = scResults.get();
			System.out.println(row[0]+", "+ row[1]);
		}
		session.close();
	}
} 
Find the output.
Hibernate: SELECT id, name FROM student
1, AA
2, BB
3, CC 

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us