Hibernate Database Operations – FirstResult and MaxResult

In this article we are going to see FirstResult and MaxResult in hibernate.

FirstResult – Suppose if we like to retrieve records from database from 6th record, then we can set setFirstResult(6).

MaxResult – Suppose if we need to retrieve only 10 records from database, then we can use setMaxResult(10).

FetchSize – This is almost similar to MaxResult, We can set number of rows to be retrieved. The difference is that optimization, Change in way of sending the results (Like chunks, buffered etc) – Not Supported by Most of the Database Drivers.

Now let us see an example,



    <property name="hibernate.hbm2ddl.auto">create</property> 
    <property name="hibernate.show_sql">true</property> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">URL</property>
    <property name="hibernate.connection.username">USERNAME</property>
    <property name="hibernate.connection.password">PASSWORD</property>
    <mapping class="com.javainfinite.pojo.Employee"></mapping>


package com.javainfinite.pojo;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

public class Employee implements Serializable {
    @Column (name = "Employee_Id")
    private int id;
    @Column (name = "Employee_Name")
    private String ename;

    public int getId() {
        return id;

    public void setId(int id) {
        this.id = id;

    public String getEname() {
        return ename;

    public void setEname(String ename) {
        this.ename = ename;


public class NewHibernateUtil {

    private static final SessionFactory sessionFactory;
    static {
        try {
            // Create the SessionFactory from standard (hibernate.cfg.xml) 
            // config file.
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Log the exception. 
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
    public static SessionFactory getSessionFactory() {
        return sessionFactory;


import com.javainfinite.pojo.Employee;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class Emp_DAO {
    Session session = NewHibernateUtil.getSessionFactory().openSession();
    public void addEmp(String empname)
        Transaction trans = session.beginTransaction();
        Employee emp = new Employee();


import com.javainfinite.pojo.Employee;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
public class E_Main {
    public static void main(String args[])
        Session session = NewHibernateUtil.getSessionFactory().openSession();
        Emp_DAO edao = new Emp_DAO();
        for(int i=1; i<=10; i++)
        Query query = session.createQuery("from Employee");
        List<Employee> elist = query.list();
        for(Employee e:elist)

Here, We are inserting 10 records into the database,


Then we are setting the FirstResult(5), It will print the records from 6. We are also setting MaxResult(3) so it should print the 3 records (Ename6, Ename7, Ename8)





By Sri

Leave a Reply

Your email address will not be published. Required fields are marked *