Java
  Home arrow Java arrow Page 3 - Hibernate: Paving the Path for Queries
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Hibernate: Paving the Path for Queries
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2005-12-20

    Table of Contents:
  • Hibernate: Paving the Path for Queries
  • Understanding the Architecture
  • Queries: Understanding the Types
  • In the Real World: Continued

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Java Articles
    More By A.P.Rajshekhar


       · This is second in the series of Hibernate. In this article I have tried to look at...
       · hi the article on hibernate and queries was really interesting and good enough to...
       · HiThank you for your comments. HQL in Depth has already been published on this...
     

    JAVA ARTICLES

    - 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 ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT