Component Mapping

The Hibernate Criteria Query Language (HCQL) is utilized to bring the records dependent on the particular criteria. The Criteria interface gives strategies to apply criteria, for example, retreiving every one of the records of table whose pay is more prominent than 50000 and so on.

Advantage of HCQL

The HCQL gives strategies to include criteria, so it is easy for the java developer to include criteria. The java software engineer can include numerous criteria a query.

Criteria Interface

The Criteria interface gives numerous strategies to indicate criteria. The object of Criteria can be gotten by calling the createCriteria() technique for Session interface.

Syntax of createCriteria() strategy for Session interface

  1. public Criteria createCriteria(Class c)  

The regularly utilized strategies for Criteria interface are as follows:

  1. public Criteria add(Criterion c) is utilized to include restrictions.
  2. public Criteria addOrder(Order o) determines ordering.
  3. public Criteria setFirstResult(int firstResult) determines the principal number of record to be retreived.
  4. public Criteria setMaxResult(int totalResult) indicates the all out number of records to be retreived.
  5. public List list() returns list containing object.
  6. public Criteria setProjection(Projection projection) determines the projection.

Restrictions class

Restrictions class gives techniques that can be utilized as Criterion. The generally utilized strategies for Restrictions class are as follows:

  1. public static SimpleExpression lt(String propertyName,Object value) sets the less than imperative to the given property.
  2. public static SimpleExpression le(String propertyName,Object value) sets the less than or equal imperative to the given property.
  3. public static SimpleExpression gt(String propertyName,Object value) sets the greater than requirement to the given property.
  4. public static SimpleExpression ge(String propertyName,Object value) sets the greater than or equal than requirement to the given property.
  5. public static SimpleExpression ne(String propertyName,Object value) sets the not equal requirement to the given property.
  6. public static SimpleExpression eq(String propertyName,Object value) sets the equal limitation to the given property.
  7. public static Criterion between(String propertyName, Object low, Object high) sets the between constraint.
  8. public static SimpleExpression like(String propertyName, Object value) sets the like limitation to the given property.

Order class

The Order class represents an order. The commonly used methods of Restrictions class are as follows:

  1. public static Order asc(String propertyName) applies the ascending order on the basis of given property.
  2. 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 great deal of instances of HCQL.

Example of HCQL to get all the records

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

Example of HCQL to get the tenth to twentieth record

  1. Crietria c=session.createCriteria(Emp.class);  
  2. c.setFirstResult(10);  
  3. c.setMaxResult(20);  
  4. List list=c.list();  

Example of HCQL to get the records whose pay is more prominent than 10000

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

Example of HCQL to get the records in rising request based on salary

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

HCQL with Projection

We can get information of a specific segment by projection, for example, name and so forth. We should see the basic case of projection that prints information of NAME section of the table only.

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





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition