Alphalearningschool
        

<<< Prev

Next >>>

Up
HCQL

HCQL (Hibernate Criteria Query Language) : The Hibernate Criteria Query Language (HCQL) is used to fetch the data based on specific criteria/condition. The Criteria interface provides the methods to get data according to condition. Ex: From a table whose salary is greater than 10000.

Advantage of HCQL :  The HCQL provides methods to add criteria. Through this we can add some criteria according to need.

Syntax of createCriteria() method of Session interface :

public Criteria createCriteria(Class c)

 

Examples of Hibernate Criteria Query Language :

 To get all the records:

Crietria c=session.createCriteria(Emp.class);//passing Class class argument  
List list=c.list();

 

 To get the 20th to 30th record:

Crietria c=session.createCriteria(Emp.class);  
c.setFirstResult(20);  
c.setMaxResult(30);  
List list=c.list();

 

To get the records whose salary is greater than 50000 :

Crietria c=session.createCriteria(Emp.class);  
c.add(Restrictions.gt("salary",50000));//salary is the propertyname  
List list=c.list();  

 

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(); 

HCQL with Projection : If we want particular field such as name than we use projection

Criteria c=session.createCriteria(Emp.class);  
c.setProjection(Projections.property("name"));  
List list=c.list();