Archive for the ‘Hibernate’ category

Paging Hibernate Query Results

July 11th, 2011

When developing applications that provide create, read, update and delete (CRUD) functionality, it is often a requirement to search on and present large data sets. This requirement is usually fulfilled by providing the ability to page through the result set, presenting a single page at a time.

For example, consider a very simple data set containing 10 integers.

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

If I want to view the second page of this data set and the page size is 3, I will expect the page to contain the following data:

{4, 5, 6}

Typically, we will also want to present the total number of results in the result set (i.e. 10) so that we can establish the total number of pages available (assuming a static result set). We will often present shortcuts to specific pages and a next\previous page link.

Now if we imagine that we scale this example, it is generally more acceptable to retrieve this data from the data store in pages, rather than retrieving the entire data set and presenting it in pages. This approach is made more compelling as the consumption of the data moves further away from the data itself (as in n-tier architectures).

I present below one way that this functionality can be supported using Hibernate Named Queries. You can download the complete source code here.

» Read more: Paging Hibernate Query Results