Hibernate Criteria Query

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

Ads

Tutorials   
How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate 4.3 Hello World: Example How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment Hibernate Architecture Hibernate Overview Hibernate 4.2 Tutorial Hibernate 4 Annotations Tutorial Hibernate 4 AnnotationSessionFactoryBean hibernate versions hibernate jndi hibernate tomcat hibernate metamodel generator hibernate entity Seam Hibernate C3P0 Hibernate hibernate shard hibernate means temporal hibernate jsf hibernate spring j2ee hibernate spring hibernate encrypted password alias_to_entity_map hibernate temporal jsf spring hibernate hibernate by example hibernate repository hibernate sample code
Ads

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.

 

Advertisements

Share on Google+Share on Google+

Hibernate Criteria Query

Posted on: March 20, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Hibernate Criteria Query  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads