For retrieving specific rows(here i am retrieving salary between 20000-30000) from  database we  are using RESTRICTIONS(criteria api) for this requirement we have to follow the following steps

Step 1: Create table from your database(i am using MYSQL )

for doing any operations in hibernate we have to  create table with primary key

create table infosys(id varchar(8),name varchar(25),designation varchar(25),email varchar(35),salary int(10));

now table is created as below






Step 2: create POJO class with table fields

package a;
public class Infosys  implements java.io.Serializable {


     private String id;
     private String name;
     private String designation;
     private String email;
     private Integer salary;

    public Infosys() {
    }


    public Infosys(String id) {
        this.id = id;
    }
    public Infosys(String id, String name, String designation, String email, Integer salary) {
       this.id = id;
       this.name = name;
       this.designation = designation;
       this.email = email;
       this.salary = salary;
    }
 
    public String getId() {
        return this.id;
    }
   
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return this.name;
    }
   
    public void setName(String name) {
        this.name = name;
    }
    public String getDesignation() {
        return this.designation;
    }
   
    public void setDesignation(String designation) {
        this.designation = designation;
    }
    public String getEmail() {
        return this.email;
    }
   
    public void setEmail(String email) {
        this.email = email;
    }
    public Integer getSalary() {
        return this.salary;
    }
   
    public void setSalary(Integer salary) {
        this.salary = salary;
    }




}



Step 3: generate hibernate configuration file (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.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/xxx</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <mapping resource="a/Infosys.hbm.xml"/>
  </session-factory>
</hibernate-configuration>


Step 4: create a class with projection logic

package a;

import java.util.ArrayList;
import java.util.Iterator;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

public class EmployeeServlet {

public static void main(String ar[]){
Configuration cfg=new Configuration();
cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session hsession=sf.openSession();
Criteria c = hsession.createCriteria(Infosys.class);
c.add(Restrictions.between("salary", 20000, 30000));
ArrayList list=(ArrayList)c.list();
Iterator itr=list.iterator();
while(itr.hasNext()){
 
   Infosys e = (Infosys)itr.next();
     
  
System.out.println(e.getId());
        System.out.println(e.getName());
        System.out.println(e.getDesignation());
        System.out.println(e.getEmail());
        System.out.println(e.getSalary());



}

}

}
 

Step 5: generate hibernate mapping file from database table 



<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Oct 8, 2013 10:36:24 AM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
    <class name="a.Employee" table="employee" catalog="xxx">
        <id name="id" type="int">
            <column name="id" />
            <generator class="assigned" />
        </id>
        <property name="name" type="string">
            <column name="name" length="30" />
        </property>
        <property name="address" type="string">
            <column name="address" length="30" />
        </property>
    </class>
</hibernate-mapping>

Step 6: before run this project i am checking infosys table from database for how many records are contain




Step 7: now i am running my project now see the out put is




if we observe above two tables,  in second table we are getting records based on salary , here the salary between 20000-30000



0 comments:

Post a Comment