Get all updates of Java4s
Follow Us: Email Newsletter

Example On Hibernate Pagination With Servlet In Eclipse

Hibernate » On Jul 21, 2011 { 11 Comments } By Sivateja

Let us see an example on hibernate pagination with servlet..

  • when response for request is too large [If we have 1000’s of records in the database] then instead of displaying all records at a time on browser  we can display the response page by page manner using pagination mechanism
  • In pagination, initially one page response will be displayed and we will get links for getting the next pages response
  • In this servelt & hibernate integration, we are going to display  4 records or 4 objects of products using hibernate for selecting the data and we will get links to display the records of the next pages

 

Regarding Logic  In Order To Get pagination

  • The servlet accepts pageIndex parameter and if the parameter is passed then servlet takes the given number as pageIndex, otherwise the servlet will takes the pageIndex as one [ 1  ]
  • Servlet uses Criteria API and the pagination methods of Criteria for loading the records (objects) related to that particular page, and servlet display those records on the browser
  • In servlet we use Criteria with projection for finding the total number of records available in the table, and we store that number into the variable
  • We will find out the number of hyperlinks required by dividing the total number of records with records per page
  • we need to use a loop in order to display the links on the browser, while creating each link,  we use the <a href  /> to servlet url pattern [Hiper reference] and by passing that page nomber as value for pageIndex parameter

 

Hibernate Pagination Example In Eclipse

Mates, let see one real time example on this hibernate pagination with servlet
files required…

  • Pagination.java
  • Product.java
  • Product.hbm.xml
  • hibernate.cfg.xml
  • web.xml

Let us see the directory structure  in the Eclipse ..

Servlet is j2ee related application, so just create one application newly unlike previous normal java programs

Open eclipse –> New –>  Dynamic Web Project

 Note:

  • src folder contains all *.java files
  • build folder contains all *.class files in side classes folder
  • Hibernate related xml’s hibernate.cfg.xml, mapping files should be in classes folder only

product.java

Product.xml

hibernate.cfg.xml

Pagination.java

Note:

  • We must create the SessionFactory object in the init() only, as it is the heavy weight one
  • and nothing to explain, just read slowly. If you got struck at any point just fire a question in our forum, and see line number 101,  ind?pageIndex (ind is my url pattern)

web.xml

Output in eclipse:

Right click on the project root — > run –> Run on Server

 

 

You Might Also Like

  ♦ About Author ♦

Java4s_Author
sivateja@java4s.com
Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

11 Responses to “Example On Hibernate Pagination With Servlet In Eclipse”
  1. Robin says:

    while i am running the program……my Pagination class is not found……plz help me

  2. Java4s says:

    @Robin

    I have tested the application and its working fine from my end, make sure you followed the the same steps, and again download the application –> import into your eclipse and test again.

    Let me know the exception you are getting, if you still have any problem.

  3. siva says:

    Really very helpful….:)

  4. tapan says:

    what does setFirstResult in criteria does

  5. Hari says:

    I am thrilled with ur logic.. Thanks for ur information:)

  6. nilesh rode says:

    while im creating database in oracle its giving error…. error is create database is falied… database is already mounted… plz give me solution on this….

  7. shaziya says:

    I am getting number format exception
    For input string: “com.exilant.beans.Product@70dbdc4b”
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

    which is this line

    String sPageIndex = request.getParameter(“pageIndex”);

  8. Anand says:

    Hi,
    This is awesome example but what if many clients connects?
    What about threading synchronization issues?
    Can I use HttpSession object to hold SessionFactory reference?
    Or can I use synchronized block?
    In dotnet for such scenarios DataTable are used which use disconnected architecture, so there will be no connection with the database. What happens in case of Hibernate? is it caching the while data in memory or fetch data from database every time? Can u please share?
    Please comment.
    Thanks.

  9. Bibek says:

    After downloading and importing it says 404 error
    please help me

  10. sandash says:

    thanks, i want same logic with Previous and next buttons

  11. mani says:

    i can’t understand this concept . so i kindly request u to explain in detail and how to work this?

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
Upcoming
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2016 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.