Java
  Home arrow Java arrow Page 3 - Hibernate: HQL 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  
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: HQL in Depth
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 42
    2006-01-10

    Table of Contents:
  • Hibernate: HQL in Depth
  • HQL: Understanding Queries
  • HQL in the Real World
  • Points to Remember

  • 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: HQL in Depth - HQL in the Real World


    (Page 3 of 4 )

    Until now I was doing all the setting up and configuration in the main(). This time I will be creating a class with a reusable and (almost) generic function. The setting up of the Hibernate will happen in the constructor. So let's begin.

    First, the usual: package name and imports:

    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

    Then the class declaration:

    public class OrderOP {
    :
    :
    }

    Now comes the constructor and the configurations:

    public class OrderOP {

    SessionFactory sf;

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

                sf = cfg.buildSessionFactory();

        }
    :
    :
    }

    The next function retrieves the data(Orders and Product) on the basis of the upper and lower priceTotals passed as arguments.

    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();
               
    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 = q.list();

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

               return o;

               }

    :
    :
    }

    And last but not least, the main function for testing:

    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();
           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 = q.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

        }

    }

    That’s it. Such a class can be used as a data processing layer’s component. In the future I will be adding more functionalities to it.

    More Java Articles
    More By A.P.Rajshekhar


       · HiIn this article I have discussed about HQL. If you have any doubts or ideas...
       · "Even the most complex queries in SQL can be mapped to HQL. " is totally a wrong...
       · I am not able to do the join in my tablesAm done with HQL query as u...
     

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