Criteria
Agenda
Criteria :
- Using criteria we can only retrieve the objects from the database. We can't perform DML operations.
- And with criteria we can retrieve complete object only. We can't retrieve partial object.
- To retrieve partial object we have to combine criteria with projections.
- To execute Criteria first we need to create Criteria object.
- To get Criteria object, we need to call createCriteria() method on Session interface. We need to pass our pojo class to this method.
- On the query object call list() method , list() will return java.util.List.
We need to iterate that list to get the required objects
Criteria criteria=session.createCriteria(Employee.class);
List list=criteria.list();
Iterator iterator=list.iterator();
while(iterator.hasNext()){
Employee emp=(Employee)iterator.next();
}
Example :
Employee.java
package com.beans;
public class Employee {
private long employeeId;
private String employeeName;
private double salary;
public long getEmployeeId() {
return employeeId;
}
public void setEmployeeId(long employeeId){
this.employeeId = employeeId;
}
public String getEmployeeName(){
return employeeName;
}
public void setEmployeeName(String employeeName){
this.employeeName = employeeName;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
Employee.hbm.xml
<hibernate-mapping>
<class name="com.beans.Employee" table="EMPLOYEEDB">
<id name="employeeId" column="EID"/>
<property name="employeeName" column="ENAME" length="30"/>
<property name="salary" column="ESAL"/>
</class>
</hibernate-mapping>
hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<!-- Related to the connection properties -->
<property name="myeclipse.connection.profile">myJdbcDriver</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">lms</property>
<property name="connection.password">scott</property>
<!-- Related to the hibernate properties -->
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<!-- Related to hibernate mapping -->
<mapping resource="com/hiber/Employee.hbm.xml" />
</session-factory>
</hibernate-configuration>
Client Application 1 :
package com.client;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.beans.Employee;
public class ClientApp {
public static void main(String ar[]){
Configuration cfg=new Configuration();
cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Criteria criteria=session.createCriteria(Employee.class);
List list=criteria.list();
Iterator iterator=list.iterator();
while(iterator.hasNext()){
Employee emp=(Employee)iterator.next();
System.out.println(emp.getEmployeeId());
System.out.println(emp.getEmployeeName());
System.out.println(emp.getSalary());
}
System.out.println("success");
}
}
Adding conditions to Criteria :
- Using Criteria if we want to add conditions to the query, then we need to create one Criterian object and add this object to Criteria.
- To add Criterion object to Criteria, we need to call add() method on criteria object.
- To create Criterion object, we need to call static methods of Restrictions class, each method of this class returns Criterion object.
Ex:
Criteria criteria=session.createCriteria(Employee.class);
Criterion criterion=Restrictions.gt("salary", new Double(3000));
//here salary is pojo class variable name
criteria.add(criterion);
Note : POJO class, Mapping and Configuration files are same as above application.
Client Application 2 :
package com.client;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import com.beans.Employee;
public class ClientApp {
public static void main(String ar[]){
Configuration cfg=new Configuration();
cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Criteria criteria=session.createCriteria(Employee.class);
Criterion criterion=Restrictions.gt("salary", new Double(3000));
criteria.add(criterion);
List list=criteria.list();
Iterator iterator=list.iterator();
while(iterator.hasNext()){
Employee emp=(Employee)iterator.next();
System.out.println(emp.getEmployeeId());
System.out.println(emp.getEmployeeName());
System.out.println(emp.getSalary());
}
System.out.println("success");
}
}
Note : POJO class, Mapping and Configuration files are same as above application.
Client Application 3 :
package com.client;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import com.beans.Employee;
public class ClientApp {
public static void main(String ar[]){
Configuration cfg=new Configuration();
cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Criteria criteria=session.createCriteria(Employee.class);
Criterion criterion=Restrictions.gt("salary", new Double(3000));
criteria.add(criterion);
Order order=Order.asc("salary");
criteria.addOrder(order);
List list=criteria.list();
Iterator iterator=list.iterator();
while(iterator.hasNext()){
Employee emp=(Employee)iterator.next();
System.out.println(emp.getEmployeeId());
System.out.println(emp.getEmployeeName());
System.out.println(emp.getSalary());
}
System.out.println("success");
}
}
Retrieving partial object with the help of Projections :
- Using Criteria we can retrieve only complete object, but it is possible to retrieve partial object if we combine Criteria with projects.
- Projection is an interface and Projections is a class.
- To get Projection object we need to call property() method on Projections class. It is static method and will return Projection object.
- To add Projection object to Criteria , we need to call setProjection() method on Criteria object.
- To retrieve partial object with single column, we need to create Projection object for that particular column value type.
Note : POJO class, Mapping and Configuration files are same as above application.
Client Application 4 :
package com.client;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import com.beans.Employee;
public class ClientApp {
public static void main(String ar[]){
Configuration cfg=new Configuration();
cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Criteria criteria=session.createCriteria(Employee.class);
Criterion criterion=Restrictions.gt("salary", new Double(3000));
criteria.add(criterion);
Order order=Order.asc("salary");
criteria.addOrder(order);
Projection projection=Projections.property("salary");
criteria.setProjection(projection);
List list=criteria.list();
Iterator iterator=list.iterator();
while(iterator.hasNext()){
Double salary=(Double)iterator.next();
System.out.println(salary);
}
System.out.println("success");
}
}
Note : if we add 2 Projection objects to Criteria then 2nd projection will override the first Projection.
- To add multiple Projection objects to Criteria, we need to create ProjectionList. To create ProjectionList, we need to call projectionList() on Projections class.
- Create multiple Projection objects and add these Projection objects to ProjectionList. Set this ProjectionList to Criteria. To set ProjectionList to Criteria we need to call setProjection() method on Criteria object.
- Finally while iterating the list, we need to type cast with Object[] because list contains partial object with multiple columns.
Note : POJO class, Mapping and Configuration files are same as above application.
Client Application 5 :
package com.client;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import com.beans.Employee;
public class ClientApp {
public static void main(String ar[]){
Configuration cfg=new Configuration();
cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();
Session session=sf.openSession();
Criteria criteria=session.createCriteria(Employee.class);
Criterion criterion=Restrictions.gt("salary", new Double(3000));
criteria.add(criterion);
Order order=Order.asc("salary");
criteria.addOrder(order);
ProjectionList projectionList=Projections.projectionList();
Projection projection1=Projections.property("employeeName");
Projection projection2=Projections.property("salary");
projectionList.add(projection1);
projectionList.add(projection2);
criteria.setProjection(projectionList);
List list=criteria.list();
Iterator iterator=list.iterator();
while(iterator.hasNext()){
Object[] values=(Object[])iterator.next();
System.out.println(values[0]);
System.out.println(values[1]);
}
System.out.println("success");
}
}
|