Hibernate - SPLessons

Chapter 36

Hibernate HCQL

Hibernate HCQL

Hibernate HCQL stands for Hibernate Criteria Query Language that utilized to select the data from the database, by using criteria query language only complete objects will be selected, but not partial objects. Using Hibernate HCQL the projection and criterion concepts it is possible to select partial objects also. The advantage of Hibernate HCQL provides more criteria’s on a query, here criteria can not perform non selection operations. Following is Hibernate HCQL the syntax to perform criteria.
Criteria crit = session.createCriteria(–Our class object–);

Following is an example to get the all records from the database.

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

Following is an example to get particular record for instance record 12 and 13.

Crietria c=session.createCriteria(Emp.class);  
c.setFirstResult(12);  
c.setMaxResult(13);  
List list=c.list();  

Following is an example to get the data where salary should me 100000.

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

Following is an example to arrange the salary in ascending order.

Crietria c=session.createCriteria(Emp.class);  
c.addOrder(Order.asc("salary"));  
List list=c.list();  

Following is an example to get partial object from the database such as address.

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

Summary

  • Hibernate HCQL – Criterion is an interface.
  • Restrictions class is used to get an criterion object.
  • org.hibernate.criterion is a package under this package criterion and restriction class available.
  • Projection is utilized to get partial objects from database.