Hibernate - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Hibernate HCQL

Hibernate HCQL

shape Description

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

shape Example

Following is an example to get the all records from the database. [java]Crietria c=session.createCriteria(Emp.class);//passing Class class argument List list=c.list(); [/java] Following is an example to get particular record for instance record 12 and 13. [java]Crietria c=session.createCriteria(Emp.class); c.setFirstResult(12); c.setMaxResult(13); List list=c.list(); [/java] Following is an example to get the data where salary should me 100000. [java]Crietria c=session.createCriteria(Emp.class); c.add(Restrictions.gt("salary",100000));//salary is the propertyname List list=c.list(); [/java] Following is an example to arrange the salary in ascending order. [java]Crietria c=session.createCriteria(Emp.class); c.addOrder(Order.asc("salary")); List list=c.list(); [/java] Following is an example to get partial object from the database such as address. [java]Criteria c=session.createCriteria(Emp.class); c.setProjection(Projections.property("address")); List list=c.list(); [/java]

Summary

shape Key Points

  • 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.