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();
exampleUser.setFirstname("Max");
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.
Next: In the Real World: Continued >>
More Java Articles
More By A.P.Rajshekhar