HCQL (Hibernate Criteria Query Language)
The
Hibernate Criteria Query Language (HCQL) is used to fetch the records based on
the specific criteria. The Criteria interface provides methods to apply
criteria such as retrieving all the records of table whose salary is greater
than 50000 etc.
Advantage of HCQL
The
HCQL provides methods to add criteria, so it is easy for the java programmer to
add criteria. The java programmer is able to add many criteria on a query.
Criteria Interface
The
Criteria interface provides many methods to specify criteria. The object of
Criteria can be obtained by calling the createCriteria() method of Session
interface.
Syntax of createCriteria() method of Session interface
public Criteria createCriteria(Class c)
The
commonly used methods of Criteria interface are as follows:
public Criteria add(Criterion c) is used to add
restrictions.
public Criteria addOrder(Order o) specifies
ordering.
public Criteria setFirstResult(int firstResult)
specifies the first number of record to be retrieved.
public Criteria setMaxResult(int totalResult)
specifies the total number of records to be retrieved.
public List list() returns list containing
object.
public Criteria setProjection(Projection projection)
specifies the projection.
Restrictions class
Restrictions
class provides methods that can be used as Criterion. The commonly used methods
of Restrictions class are as follows:
public static SimpleExpression lt(String propertyName,Object
value) sets
the less than constraint to the given property.
public static SimpleExpression le(String propertyName,Object
value)
sets the less than or equal constraint to the given property.
public static SimpleExpression gt(String propertyName,Object
value)
sets the greater than constraint to the given property.
public static SimpleExpression ge(String propertyName,Object
value) sets the greater than or equal than constraint to the given
property.
public static SimpleExpression ne(String propertyName,Object
value)
sets the not equal constraint to the given property.
public static SimpleExpression eq(String propertyName,Object
value)
sets the equal constraint to the given property.
public static Criterion between(String propertyName, Object low,
Object high) sets the between constraint.
public static SimpleExpression like(String propertyName, Object
value)
sets the like constraint to the given property.
Order class
The
Order class represents an order. The commonly used methods of Restrictions
class are as follows:
public static Order asc(String propertyName) applies
the ascending order on the basis of given property.
public static Order desc(String propertyName) applies
the descending order on the basis of given property.
Examples of Hibernate Criteria Query Language
There
are given a lot of examples of HCQL.
Example of HCQL to get all the records
Criteria
c=session.createCriteria(Emp.class);//passing Class class argument
List
list=c.list();
Example of HCQL to get the 10th to 20th record
Crietria
c=session.createCriteria(Emp.class);
c.setFirstResult(10);
c.setMaxResults(20);
List
list=c.list();
Example
of HCQL to get the records whose salary is greater than 10000
Crietria
c=session.createCriteria(Emp.class);
c.add(Restrictions.gt("salary",10000));//salary
is the property name
List
list=c.list();
Example
of HCQL to get the records in ascending order on the basis of salary
Crietria
c=session.createCriteria(Emp.class);
c.addOrder(Order.asc("salary"));
List
list=c.list();
Employee.java
=========
package
mypack;
import
javax.persistence.*;
@Entity
@Table(name
= "MYEMP")
public
class Employee {
@Id
private
int id;
private
String name;
private
float salary;
private
String address;
public
Employee() {
super();
}
public
Employee(String name, float salary,String address) {
super();
this.name = name;
this.salary = salary;
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;
}
public
float getSalary() {
return salary;
}
public
void setSalary(float salary) {
this.salary = salary;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
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="connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
<property
name="dialect">org.hibernate.dialect.DerbyDialect</property>
<property
name="connection.url">jdbc:derby://localhost:1527/myhb</property>
<property
name="connection.username">app</property>
<property name="connection.password">app</property>
<property
name="show_sql">true</property>
<property
name="hbm2ddl.auto">update</property>
<mapping
class="mypack.Employee"/>
</session-factory>
</hibernate-configuration>
SelectQuery.java
=========
package
mypack.methods;
import
mypack.*;
import
java.util.*;
import
org.hibernate.*;
import
org.hibernate.cfg.*;
public
class SelectQuery {
public static void main(String[] args) {
SessionFactory sessionFactory = new
AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();
Session
session = sessionFactory.openSession();
Criteria cr = session.createCriteria(Employee.class);
List list = cr.list();
Iterator itr = list.iterator();
while(itr.hasNext()){
Employee emp = (Employee)itr.next();
System.out.println(emp.getId()+" "+emp.getName()+"
"+emp.getSalary()+" "+emp.getAddress());
}
session.close();
}
}
PageNation.java
=========
package
mypack.methods;
import
mypack.*;
import
java.util.*;
import
org.hibernate.*;
import
org.hibernate.cfg.*;
public
class PageNation {
public static void main(String[] args) {
SessionFactory sessionFactory = new
AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();
Session
session = sessionFactory.openSession();
Criteria cr =
session.createCriteria(Employee.class);
cr.setFirstResult(2);
cr.setMaxResults(2);
List list = cr.list();
Iterator itr = list.iterator();
while(itr.hasNext()){
Employee emp = (Employee)itr.next();
System.out.println(emp.getId()+" "+emp.getName()+"
"+emp.getSalary()+" "+emp.getAddress());
}
session.close();
}
}
Methods.java
=======
package
mypack.methods;
import
mypack.*;
import
java.util.*;
import
org.hibernate.*;
import
org.hibernate.cfg.*;
import
org.hibernate.criterion.Restrictions;
public
class Methods {
public static void main(String[] args) {
SessionFactory sessionFactory = new
AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory();
Session
session = sessionFactory.openSession();
Criteria cr = session.createCriteria(Employee.class);
cr.add(Restrictions.ge("salary", 9000f));
cr.add(Restrictions.ilike("name", "Mohan"));
List list = cr.list();
Iterator itr = list.iterator();
while(itr.hasNext()){
Employee emp = (Employee)itr.next();
System.out.println(emp.getId()+" "+emp.getName()+"
"+emp.getSalary()+" "+emp.getAddress());
}
session.close();
}
}
No comments:
Post a Comment