Monday, December 2, 2013

Database level Pagination with JPA

Overview

Pagination is a presentation technique used to show large amount of data set in client UI side as set of pages with set of options like next,previous,first last etc. 

As an example think a scenario of getting employee information to web page. If you have 1000 employees loading all those data and reading them become tricky task. So we can load employee data 10 by 10 to web page. In common this is done by sending simple SELECT query to database and get resultset and then do paging based on page number and size in the presentation level. This cause some performance issues in production level . When accessing remote database and getting large data set cause network bandwidth issues. Processing large data in UI level will cause more issues relate to performance. When you try to add all those paging limits to query it self will make query more complex. JPA provides simple two DB level methods to do pagination without affecting the basic query.
Assume you have declared pageSize and pageNumber variables.

Query query = session.createQuery('your query'); query.setMaxResults(pageNumber); query.setFirstResult(pageSize*pageNumber); 

You can store pageSize and pageNumber in label or hidden value in UI level.

No comments :

Post a Comment