For retrieving specific column from database we are using PROJECTION(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 employee(id int(10) primary key(id),name varchar(30),address varchar(30));
now table is created as below
Step 2: create POJO class with table fields
package a;
public class Employee {
int id;
String name;
String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
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();
//c.add(Restrictions.between("salary", 20000, 34000));
//c.addOrder(Order.asc("id"));
Criteria c = hsession.createCriteria(Employee.class);
ProjectionList pl = Projections.projectionList();
pl.add(Projections.property("name"));
c.setProjection(pl);
ArrayList list=(ArrayList)c.list();
Iterator itr=list.iterator();
while(itr.hasNext()){
String e = (String)itr.next();
System.out.println(e);
}
}
}
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 employee(id int(10) primary key(id),name varchar(30),address varchar(30));
now table is created as below
Step 2: create POJO class with table fields
package a;
public class Employee {
int id;
String name;
String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
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/Employee.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();
//c.add(Restrictions.between("salary", 20000, 34000));
//c.addOrder(Order.asc("id"));
Criteria c = hsession.createCriteria(Employee.class);
ProjectionList pl = Projections.projectionList();
pl.add(Projections.property("name"));
c.setProjection(pl);
ArrayList list=(ArrayList)c.list();
Iterator itr=list.iterator();
while(itr.hasNext()){
String e = (String)itr.next();
System.out.println(e);
}
}
}
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: now run the application we will get
Step 6: now run the application we will get
0 comments:
Post a Comment