Home arrow Java arrow Page 3 - Hibernate: HQL in Depth
JAVA

Hibernate: HQL in Depth


SQL can be combined with an object-oriented approach. What we end up with is Hibernate Query Language, or HQL. This article discusses how HQL works, and shows how to use it in the real world.

Author Info:
By: A.P.Rajshekhar
Rating: 4 stars4 stars4 stars4 stars4 stars / 86
January 10, 2006
TABLE OF CONTENTS:
  1. · Hibernate: HQL in Depth
  2. · HQL: Understanding Queries
  3. · HQL in the Real World
  4. · Points to Remember

print this article
SEARCH DEVARTICLES

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.


blog comments powered by Disqus
JAVA ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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