2:27 am - Friday April 19, 2019

Hibernate Pagination CreateCriteria Using Eclipse

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

•  Pagination – Retrieve specified number of rows in a table.
•  This is using createCriteria.
•  For ex Query:
o  Criteria c=s.createCriteria(Student.class).add(Restrictions.like(“degree”, “BE”))
o  c.setFirstResult(0);
o  It specifies starting row from which row we want to display.
o  c.setMaxResults(5);
o  It specifies ending row to display.

 

Project Structure:              

 

 

 

 

 

 

  

 

 

 

JarFiles:

 

 

  

 

 

 

 

 

 

 

 

PaginationCreatecritiria.java:

package com.candidjava;

import java.util.*;

public class PaginationCreatecritiria {
	public static void main(String args[]) {
		try {
			PaginationCreatecritiriaControl ctrl = new PaginationCreatecritiriaControl();
			List li = ctrl.param(args);
			for (Iterator iter = li.iterator(); iter.hasNext();) {
				Student stu = (Student) iter.next();

				tem.out.println("ID: " + stu.getId());
				System.out.println("StuId: " + stu.getStuid());
				System.out.println("StuRegNo: " + stu.getSturegno());
				System.out.println("StuName: " + stu.getStuname());
				System.out.println("StuMark1: " + stu.getStumark1());
				System.out.println("StuMark2: " + stu.getStumark2());

				System.out.println("Degree: " + stu.getDegree());
				System.out.println("Phone: " + stu.getMobileno());
				System.out.println("Mailid: " + stu.getMailid());
				System.out.println("");
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}

 PaginationCreatecritiriaControl.java:

 

package com.candidjava;

import java.io.DataInputStream;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Restrictions;

public class PaginationCreatecritiriaControl {
	private static SessionFactory sessionFactory;

	private Session getSession() {
		Session s = null;
		try {
			sessionFactory = new Configuration().configure(
					"com\\xml\\hibernate.cfg.xml").buildSessionFactory();
			s = sessionFactory.openSession();
		} catch (HibernateException e) {
			System.out.println(e.getMessage());
		}
		return s;
	}

	public List param(String[] args) throws Exception {
		List list = null;
		try {
			Session s = getSession();
			DataInputStream ds = new DataInputStream(System.in);
			System.out.println("*********************\n");
			System.out.println("Enter the Degree to Search");
			String first = ds.readLine();

			Criteria c = s.createCriteria(Student.class).add(
					Restrictions.like("degree", first));

			c.setFirstResult(0);
			c.setMaxResults(5);
			list = c.list();
		} catch (HibernateException e) {
			System.out.println(e.getMessage());
		}
		return list;
	}
}

 Student.java: 

package com.candidjava;

import java.io.*;

public class Student implements Serializable {
	private long id;
	private String stuid;
	private String sturegno;
	private String stuname;
	private String stumark1;
	private String stumark2;
	private String degree;
	private String mobileno;
	private String mailid;

	public long getId() {
		return id;
	}

	public String getStuid() {
		return stuid;
	}

	public String getSturegno() {
		return sturegno;
	}

	public String getStuname() {
		return stuname;
	}

	public String getStumark1() {
		return stumark1;
	}

	public String getStumark2() {
		return stumark2;
	}

	public String getDegree() {
		return degree;
	}

	public String getMobileno() {
		return mobileno;
	}

	public String getMailid() {
		return mailid;
	}

	public void setId(long string) {
		id = string;
	}

	public void setStuid(String string) {
		stuid = string;
	}

	public void setSturegno(String string) {
		sturegno = string;
	}

	public void setStuname(String string) {
		stuname = string;
	}

	public void setStumark1(String string) {
		stumark1 = string;
	}

	public void setStumark2(String string) {
		stumark2 = string;
	}

	public void setDegree(String string) {
		degree = string;
	}

	public void setMobileno(String string) {
		mobileno = string;
	}

	public void setMailid(String string) {
		mailid = string;
	}

}

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>paginationCreateCritiria</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

hibernate.cfg.xml:

<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory name="studentFactory">
		<property name="connection.driver_class">
			oracle.jdbc.OracleDriver
	        </property>
		<property name="connection.url">
			jdbc:oracle:thin:@localhost:1521:XE
		</property>
		<property name="connection.username">
			system
		</property>
		<property name="connection.password">
			system
		</property>
		<property name="connection.pool_size">5</property>
		<!-- SQL dialect -->
		<property name="dialect">
			org.hibernate.dialect.OracleDialect
	        </property>
		<!-- Echo all executed SQL to stdout -->
		<property name="show_sql">false</property>
		<property name="hbm2ddl.auto">update</property>
		<mapping resource="\com\\xml\\Student.hbm.xml" />
	</session-factory>
</hibernate-configuration>

Student.hbm.xml: 

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
	"-//Hibernate/Hibernate Mapping DTD//EN"
	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.candidjava.Student" table="Stude">
		<id name="id" type="long" column="Id">
			<generator class="increment" />
		</id>
		<property name="stuid" column="StuId" not-null="true" />
		<property name="sturegno" column="StuRegNO" />
		<property name="stuname" column="StuName" />
		<property name="stumark1" column="StuMark1" />
		<property name="stumark2" column="StuMark2" />
		<property name="degree" column="Degree" />

		<property name="mobileno" column="MobileNo" />
		<property name="mailid" column="MailId" />
	</class>
</hibernate-mapping>

OUTPUT:

 

 

 

  

 

 

 

 

 

 

 

 

 

   

 

 

 

 

 

 

 

  

 

 

 DOWNLOAD+SOURCECODE

BACK

Filed in: Z-A Hibernate codes

No comments yet.

Leave a Reply