Hello, how are you?
Today we will do JSF datatable pagination with a Lazy List with a view scoped managed bean. What all those words/expressions mean?
There are several JSF frameworks today that provide datatables with a ready pagination, columns sorter and others functionalities. Today we will use the Primefaces datatable.
Usually the datatables will put the displayed List, with the entities, inside the user http session. Increasing the objects inside the user session will have a direct impact on the server performance; each user that displays a datatable, that keeps a list in the session, will be allocating more and more memory in the server.
To look like real life, our post will use JPA and HSQLDB as database, and we will use JPQL to query the data.
In the end of this post you will find the link to download the source code.
We will use:
- JSF 2.0 – JBoss 7 Implementation
- JBoss 7.1 – the code of this post should be applied to all servers
- Eclipse Indigo
- JPA 2.0 – JBoss 7 Implementation
- HSQLDB (2.2.8) – The HSQL it is a memory database, it will be easier to run it.
- Primefaces 3.2
This post is not about good development practices nor about adding classes layers of project modeling. I just intend to show how to do a pagination without a session managed bean.


hello I have a problem. I made some adjustments in your tutorial
with respect to the dao
But it shows me this error
Grave: Error Rendering View [/ index.xhtml]
java.lang.ArithmeticException: / by zero
at org.primefaces.model.LazyDataModel.setRowIndex (LazyDataModel.java: 62)
say what can I do to remedy this problem.
from already thank you and congratulations for the tutorial.
Hello
I am sorry but I cannot help you now.
I do not have free time to look at you code. I would need to check all your code to see where the problem is.
Post your code/doubt here: stackoverflow.com
You will find good people that gives free consulting based on your code.
Thanks for passing by.
Man.. you didn´t implement the filtering part of the method LOAD.
Why not? :(
Hello Marco
Yes, I did not implement the filter because it is very straightforward.
I talked about how to use it, but I did not put any code to leave the post more simple.
If you have any doubt about how to use it, you can do System.out.println in the parameters to see the value that is arriving from the view.
Check the values and use it in the query.
Thanks for passing by.
check out the above link of the question that i posted in stackoverflow.com some time ago , maybe that will help you
Hello Jose Maria,
Thanks for helpping here. [=
Hi again ,first for the problem that i had it was a missing .jar in glassfish folder this is new since gf 3.1.2,
in the other hand , i succesufully implemented lazy loading for a big query that i had , now the issue is how to implement a global filter for the datatable knowing that the datatable only has the data for an specific range. have you done this before? any tips for this ? thanks in advance
Jose Maria.
Hello Jose,
I believe that the query that you will be using the pagination would filter the data.
Let us image that the following query would bring 200.000.000 results:
select p from PersonYou would keep the same query but the pagination would do the trick to filter that. When you use the setFirstResult() and the setMaxResults() methods you will be filtering the select result.
I hope that helps you.
Thanks for passing by.
hi thanks for your answer :D ,i dont know how to do it yet so i post a question in stackoverflow.com here is the link
http://stackoverflow.com/questions/11235952/implement-global-filter-in-a-primefaces-datatable-using-lazy-loading
Hello Jose Maria,
You are welcome, good luck.
@Jose Maria:
hi again , i was able to resolve the above issue, i now show my query in the datatable , but i can only show the first page of my data , when i click another page i got an exception at this line
return query.getResultList(); (this only works at the first time that i run the page)
I use EJB and use cdi lookup to have access to my sessionBean from the lazy class .
and i got this exception
Caused by: java.lang.NoClassDefFoundError: oracle/jdbc/driver/ResultSetUtil
it apparently doesnt read the library but dont know why , if at the first time i can execute the query
any ideas what im doing wrong? Thank you :)
Hello Jose Maria,
Did you created the driver as a module? Maybe that is your problem.
Are you using jboss 7?
See ya! \o_
Hola , thanks for your answer, i really dont know what you mean to create my driver as a module, i dont use jboss , i use glassfish 3.1 the one that is integrated with netbeans 7,
gracias, :D
Hello Jose Maria.
Modules is a concept of JBoss 7.
Follow this step to set up the oracle for glassfish: http://trycatchfinally.blogspot.com.br/2006/05/creating-oracle-datasource-in.html
If the link is of no help, post your doubt in here: http://www.coderanch.com
You will find wise people in there. They will help you.
Thanks for passing by and I am sorry for the late response.
Hi uaihebert,
You might be interested in this implementation of a paged Lazy List which can be used wherever a List is allowed.
http://www.ninthavenue.com.au/blog/paging-large-data-sets-with-a-lazylist
Obrigado,
Roger
Hello Roger,
This is a good way to implement a lazy pagination.
The problem is that the developer looses the JSF auto complete that help a lot on the delopment process.
Thanks for the link. [=
Hi, I am a junior developer, I am very interesting in Primefaces library and JSF 2.0. I say the Primefaces. it have good features and code examples and vidoetutorials, that i permits to learn easy and fast.
Today, I watch your post, it is very interesting and very good quality. i want to share a videotutorial about DataTable of Primefaces 3.2 with common features to help with the learning of this framework.
Datatable Primefaces 3.2 with sort, filter, select row with display info,
http://www.youtube.com/watch?v=z8EkoJtum3c&list=PLF6FC278D63382D51&index=3&feature=plpp_video
I hope not upset with my opinion and leave a list of interesting tutorials of youtube
thanks.
Hello Fahyos.
There is no problem to leave your link in here.
Thanks for passing by.
Thank you very much
This is the first tutorial about the primefaces lazy datatable on the net .
Hello,
Thanks for passing by! =D
You are welcome.
Hello you too. I find artical but does not have to say anything. I think missing contents of this? Yes?
Hello.
For some reason there was a little delay in the blog.
But now is all ok.
Sorry for the trouble and the late response.