Home arrow Java arrow Page 3 - Hibernate: Paving the Path for Queries

Hibernate: Paving the Path for Queries

In the second part of our series on Hibernate, we will take a close look at its architecture and core classes. We will also cover the different types of queries possible in Hibernate. Finally, we'll use a real world example to illustrate our discussion.

Author Info:
By: A.P.Rajshekhar
Rating: 4 stars4 stars4 stars4 stars4 stars / 14
December 20, 2005
  1. · Hibernate: Paving the Path for Queries
  2. · Understanding the Architecture
  3. · Queries: Understanding the Types
  4. · In the Real World: Continued

print this article

Hibernate: Paving the Path for Queries - Queries: Understanding the Types
(Page 3 of 4 )

Hibernate provides three categories of Queries. These are:

1. Hibernate Query Language

2. Query By Criteria

3. Query By Example

Though all of these work on objects and return objects, the second type is more aimed towards the Object Oriented paradigm. The reason for this is in the details below.

1. Hibernate Query Language

Also known as HQL,  this is an object oriented dialect similar to the relational dialect of SQL. It works on persistent objects. It looks similar to EJB Query Language or EJBQL. The major difference is that EJBQL (in its version 2.0) supports only the Retrieve operation of CRUD quad, whereas using HQL all of these operations can be done. The HQL query goes into the createQuery() method of session as a string parameter. The main functionalities provided by HQL include:

  • Application of restrictions on the properties of associated objects held together by reference or held in collection. 
  • The ability to order the results. Ordering means retrieving in ascending or     descending order.
  • Applying pagination on the result retrieved.

For example the following query retrieves all the orders:

Query q = session.createQuery("from Order");

List result = q.list();

The list() method return an instance of List containing the objects of Order. It is clear from the above example that the HQL query is similar to that of SQL. Hence understanding and using it is quite easy.

2. Query By Criteria

Known also as QBC, this is the totally object oriented representation of the SQL Query. In this case, no query comes into the picture. The query is created internally. The developer works in an object oriented way. The restrictions are placed using filters, which are nothing but methods of the Criterion class. The criteria is passed as a parameter which has to be an instance of the Expression class. The following example will make this more clear:


    Criteria criteria = session.createCriteria(Order.class);

    criteria.add( Expression.eq("id", "I009") );

    List result = criteria.list();


The first visible difference is the way an instance of Criteria is obtained. Instead of  Query, the name of the class on which the query has to be executed is provided. Then a Criterion is added using the add() method of the Criteria class. The parameter is the eq() method of Expression class. The last part is the same as that of HQL.

3. Query By Example:

The long form of QBE, it is not much different from QBC. The difference is that QBC works on the restrictions representing the where clause, whereas QBE works with only the example part of the restriction. The example part is popularly known as the like condition, such as like ‘I%’ to search for a value starting with I. The main idea behind QBE is that the application provides an instance of the class to be queried, with the value of a property set to any default value. The query when executed returns all the objects with matching property value. For example,

User exampleUser = new User();
Criteria criteria = session.createCriteria(User.class);
criteria.add( Example.create(exampleUser) );
List result = criteria.list();

will return all the persistent objects containing the first name “Max”.

That brings us to the end of this section. Each of these will be discussed in detail in the coming parts. In the next section I will be using the application developed in the previous tutorial to provide the real world use of all the three types.

blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials