Hibernate Criteria Query

In this tutorial you will learn about the Hibernate Criteria Query.

Hibernate Criteria Query

Hibernate Criteria Query

In this tutorial you will learn about the Hibernate Criteria Query.

Hibernate provides an API for writing the dynamic query in an elegant way to execute . Except the use of HQL in Hibernate, Criteria query is an another way to write query it provides a more object orientation feature. When using a Criteria Query an application developer will required to use the provided API this is because when an application developer uses the HQL they must have the knowledge of SQL queries but the Criteria Query APIs provided by Hibernate maps the SQL functionality to the objects.

In applications the following native APIs are required to use :

  • Criteria : A simplified API that retrieve entities from the contained or composed Criterion objects. This approach is very useful for the "search" function when a different number of fields are composed by the restrictions query.
  • public interface Criteria extends CriteriaSpecification

    for example

    Criteria criteria = session.createCriteria(Employee.class);
    criteria.add(Restrictions.like("empName", "%n%"));
    criteria.setMaxResults(15);
    List emp = criteria.list();

    Criteria interface provides the various methods some of them are as follows :

    Methods Description
    add(Criterion criterion) adds a Criterion to constrain the results to be retrieved.
    addOrder (Order order) Order to the result set is added.
    createAlias () Join an association, assigning an alias to the joined entity.
    createCriteria () Used for creating a new Criteria, "rooted" at the associated entity.
    setMaxResults (int maxResult) Used to set a maximum limit upon the number of objects to be fetched.
    uniqueResult() Used to instruct the Hibernate to retrieve and return the unique records from database.

  • Criterion : A class may implement this interface for specifying the custom restriction criteria. In Criteria query a Criterion query may be used as a Restriction.
  • public interface Criterion extends Serializable

    Criterion interface provides the following methods :

    Method Description
    getTypedValues() Use to give back the typed values for all parameters.
    toSqlString() gives the SQL fragment.

  • Restrictions : This class allows for the built-in criterion types using the static factory methods of this class.
  • public class Restrictions extends Object

    This class provides the static methods some of them are as follows :

    Method Description
    allEq() Used to apply an "equals" constraint to each property in the key set of a Map
    and() Gives the conjunction of two expressions
    or() Gives the disjunction of two expressions
    not() Gives the negation of an expression
    in() Used to apply an "in" constraint to the specified property
    ge() Used to apply a "greater than or equal" constraint to the specified property
    le() Used to apply a "less than or equal" constraint to the specified property

  • Projections : This class allows for the built-in projection types using the static factory methods of this class.
  • public final class Projections extends Object

    This class provides the static methods some of them are as follows :

    Method Description
    alias() Using this method a projection is to be assigned an alias by wrapping it
    avg() Returns an average value of a property.
    count() Returns a count of a property.
    id() It specifies the projected identifier value

  • Order : This class specifies an order that is to be enforced upon a result set of Criteria.
  • public class Order extends Object implements Serializable

    This class provides methods to imposed the order upon result set some of them are as follows :

    Method Description
    asc(String propertyName) This is used to arrange the result set in ascending order.
    desc(String propertyName) This is used to arrange the result set in descending order.