Java
  Home arrow Java arrow Page 4 - Hibernate: Criteria Queries in Depth
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  
Moblin 
JMSL Numerical Library 
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: Criteria Queries in Depth
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 61
    2006-03-15

    Table of Contents:
  • Hibernate: Criteria Queries in Depth
  • The Core Classes of Criteria Queries
  • Using Criteria Queries
  • A Criteria Query in the Real World

  • 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: Criteria Queries in Depth - A Criteria Query in the Real World


    (Page 4 of 4 )

    Now it is time to apply what we have discussed up till now. In this section I will be rewriting the application developed in the previous article (HQL in Depth). Almost all the code is same except the query part. So the details are as below:

    package com.someorg.persist.op;

    import java.util.List;

    import org.hibernate.Hibernate;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    import com.someorg.persist.Order
    import org.hibernate.*;
    import org.hibernate.criterion.*;

    public class OrderOP {

    SessionFactory sf;

         public OrderOP(){
                       Configuration cfg = new Configuration()
                               .addClass(Order.class);

                       sf = cfg.buildSessionFactory();

               }         

              public Order getOrder(String lower, String upper){
                            // open session
                           Session sess = sf.openSession();

                         //The following code has been commented so that
                       //comparison between HQL and Criteria Query

                    /*String query = "select o from o "
                   + "Order as o join o.products as p "
                   + "where o.priceTotal > :priceTotalLower "
                   + "and o.priceTotal < :priceTotalUpper";           

                   Query q = sess.createQuery(query);
                   q.setDouble("priceTotalLower", 
                                                    Double.parseDouble(lower));

                  q.setDouble("priceTotalUpper",
                                                Double.parseDouble(upper));
                        */
                 List list = sess.createCriteria(Order.class)
                            .add(Restrictions.between(lower,upper)
                            . list();

                Order o=(Order)list.iterator.next();

               return o;
               }

              public static void main(String args[]){
                         Order o=OrderOP().getOrder(“2000’,”3000”);                 

                         System.out.println(“Order Id:”+ o.id);
                         //and so on
               }
    }

    You can see from the above code that if the object-oriented approach is really clear, then Criteria APIs are the easiest mode of data retrieval. That brings us to the end of this discussion. However, Hibernate is not just about CRUD operations. It is also about transaction and entity management as well as optimization techniques. These are topics that will be covered in the future.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · HiHQL is not suitable at all times. Thats why Criteria APIs were introduced. In...
       · This article is helpful.But your example doesn't elaborate on the HAVING?I would...
       · Thats exactly what I was thinking. He did actually reproduce that query using the...
       · Having is not supported by Hibernate 2.x and in 3.1 if the criteria after having is...
     

    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 1 hosted by Hostway
    Stay green...Green IT