Anand
Anand

Reputation: 21320

Fetching specific number of records not working in Hibernate

I have a table which contains 1000 records.

I needed to pull 100 records at a time so I used setFetchSize in the hibernate Criteria as below(Deal is the entity object)

 List<Deal> dealList= sess.createCriteria(Deal.class).setFetchSize(100).list();

But, after the above query when I printed the dealList size as

System.out.println("no. of deals "+dealList.size());

it gave 1000 records not 100 records.

Did I miss anything?

Upvotes: 0

Views: 920

Answers (3)

Sumit Desai
Sumit Desai

Reputation: 1760

use setMaxResults(100) to restrict number of rows fetched to 100

Upvotes: 0

Aleksandr M
Aleksandr M

Reputation: 24396

Use setMaxResults with setFirstResult to get next records.

Update

From java.sql.Statement#setFetchSize javadoc:

Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when more rows are needed for ResultSet objects genrated by this Statement. If the value specified is zero, then the hint is ignored. The default value is zero.

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272247

You will need setMaxResults().

Fetch size is a JDBC optimisation and not a restriction on the query performed. See this thread for more info on setFetchSize() and setMaxSize(), and this thread for how the two can work together for you.

Upvotes: 3

Related Questions