Home » Tutorial

Sample HQL Query

Written By: techieDan on January 21, 2009 3 Comments

For those Java like programmers, they will be using pure SQL statements to get the whole of a table by typing the typical SQL query statement.

String SQL = "select * from tablename where condition = ?";

From then onwards it can be passed to a POJO to be declared but everything has to be done one by one where each POJO object is set one by one. I have also written a simple complex SQL call in Java previously here. Now what if you had a hibernate mode setup, it’ll be just one time setup and you’ll get it redirect to the POJO it was meant to be.

Though this time, I’ll be talking about how HQL Query is somewhat similar to the above SQL query.

String SQL = " from tableName where condition = ?";
HibernateTemplate hibernate =
new HibernateTemplate(this.getSessionFactory());
List list = hibernate.find(SQL,"condition");

Now in just 2 lines I can call a SQL query in a simple HQL Query. Now how simple is Hibernate, if you would like to know more about hibernate, visit hibernate.org to learn about it’s framework.

Related Posts with Thumbnails

Tags: , ,

Digg this!Add to del.icio.us!Stumble this!Add to Techorati!Share on Facebook!Seed Newsvine!Reddit!Add to Yahoo!

3 Responses to “Sample HQL Query”

  1. Lasker says on: 21 January 2009 at 3:57 pm

    Wah .. Respect to TechieDan! 
    I think .NET has something similar to this .. called SQL client object .. and I dont think they have a specific name to call it.

    Basically, it enhanced the limited SQL statement and fully utilized .NET’s flexibility.

    I also know one in SAP where an SQL statement can be made into a loop swt.
    Eg. Select * from blabla where blabla=blabla
    Do your looping stuff here (swt!!)
    End Select

  2. Murali says on: 19 February 2009 at 10:05 pm

    how can i use if else condition inside  HQL querry for example i need to retrive names starting with particular letter else i need to retrive all the names..

    Thanks in advance
    Murali
    Aavanor systems

    • techieDan says on: 25 February 2009 at 11:05 am

      Hello,
      you can try something regarding to that by adding a like statement just like how one does in SQL

      String query = ” from tableName where name like ‘?%’ ”

      I think you can use this query. This was how I test the query.

      String variable = “A”;
      String query = ” from tableName where name like ‘”+variable+”%’”;

      So far it worked for me. Basically it’s just like a normal SQL.

Leave a Reply:

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  Copyright © 2009 TechieDan, All rights reserved.| Powered by WordPress