Hibernate Database Operations – FirstResult and MaxResult

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,

structure1

hibernate.cfg.xml

Employee.java

NewHibernateUtil.java

Emp_DAO.java

E_Main.java

Here, We are inserting 10 records into the database,

op1

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)

op2

 

 

 

Leave a Reply

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