Hibernate

Pagination Example using Servlet and Hibernate..

Pagination.Java

package com.student.pagination;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Projections;

public class Pagination extends HttpServlet
{

SessionFactory factory;

//init method started
public void init(ServletConfig config)throws ServletException
{
factory = new Configuration().configure().buildSessionFactory();
System.out.println("Factory has been created....");
}
//init method end

//service method start
public void service(ServletRequest req, ServletResponse res)
throws ServletException,IOException
{
int pageIndex = 0;
int totalNumberOfRecords = 0;
int numberOfRecordsPerPage = 4;

String sPageIndex = req.getParameter("pageIndex");

if(sPageIndex ==null)
{
pageIndex = 1;
}else
{
pageIndex = Integer.parseInt(sPageIndex);
}

Session ses = factory.openSession();
int s = (pageIndex*numberOfRecordsPerPage) -numberOfRecordsPerPage;

Criteria crit = ses.createCriteria(Product.class);
crit.setFirstResult(s);
crit.setMaxResults(numberOfRecordsPerPage);

List l = crit.list();
Iterator it = l.iterator();

PrintWriter pw = res.getWriter();
pw.println("<table border=1>");
pw.println("<tr>");
pw.println("<th>PID</th><th>PNAME</th><th>PRICE</th>");
pw.println("</tr>");

while(it.hasNext())
{
Product p = (Product)it.next();
pw.println("<tr>");
pw.println("<td>"+p.getProductId()+"</td>");
pw.println("<td>"+p.getProName()+"</td>");
pw.println("<td>"+p.getPrice()+"</td>");
pw.println("</tr>");
}

pw.println("<table>");

Criteria crit1 = ses.createCriteria(Product.class);
crit1.setProjection(Projections.rowCount());

List l1=crit1.list();

// pw.println(l1.size());
//returns 1, as list() is used to execute the query if true will returns 1

Iterator it1 = l1.iterator();

if(it1.hasNext())
{
Object o=it1.next();
totalNumberOfRecords = Integer.parseInt(o.toString());
}

int noOfPages = totalNumberOfRecords/numberOfRecordsPerPage;
if(totalNumberOfRecords > (noOfPages * numberOfRecordsPerPage))
{
noOfPages = noOfPages + 1;
}

for(int i=1;i<=noOfPages;i++)
{
String myurl = "ind?pageIndex="+i;
pw.println("<a href="+myurl+">"+i+"</a>");
}
ses.close();
pw.close();

}
//service method end

//destroy method start
public void destroy()
{
factory.close();
}
//destroy end
}
Product.java
package com.student.pagination;

public class Product {
    private int productId;
    private String proName;
    private double price;

    public void setProductId(int productId)
    {
        this.productId = productId;
    }
    public int getProductId()
    {
        return productId;
    }

    public void setProName(String proName)
    {
        this.proName = proName;
    }
    public String getProName()
    {
        return proName;
    }

    public void setPrice(double price)
    {
        this.price = price;
    }
    public double getPrice()
    {
        return price;
    }

}
Hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
  <property name="hibernate.connection.driver_class">  oracle.jdbc.driver.OracleDriver</property>
    <property name="hibernate.connection.username">scott</property>
    <property name="hibernate.connection.password">tiger</property>
    <property name="hibernate.connection.pool_size">10</property>
    <property name="show_sql">true</property>
    <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <property name="hibernate.cache.use_second_level_cache">true</property>
    <mapping resource="Product.hbm.xml"></mapping>
   
   </session-factory>
</hibernate-configuration>

Product.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.student.pagination.Product" table="products">

<id name="productId" column="pid"  />
<property name="proName" column="pname" length="10"/>
<property name="price"/>

</class>
</hibernate-mapping>
web.xml


No comments:

Post a Comment