Hibernate Query Language Introduction

So far we done the operations on single object (single row), here we will see modifications, updates on multiple rows of data (multiple objects) at a time.  In hibernate we can perform the operations on a single row (or) multiple rows at a time, if we do operations on multiple rows at once, then we can call this as bulk operations.

  • HQL is the own query language of hibernate and it is used to perform bulk operations on hibernate programs
  • An object oriented form of SQL is called HQL
  • here we are going to replace table column names  with POJO class variable names and table names with POJO class names in order to get HQL commands

 

Advantages Of HQL:

  • HQL is database independent, means if we write any program using HQL commands then our program will be able to execute in all the databases with out doing any further changes to it
  • HQL supports object oriented features like InheritancepolymorphismAssociations(Relation ships)
  • HQL is initially given for selecting object from database and in hibernate 3.x we can do DML operations ( insert, update…) too
  • Different Ways Of Construction HQL Select

  • If we want to select a Complete Object from the database, we use POJO class reference in place of   *  while constructing the query
  • In this case (select a complete object from the database) we can directly start our HQL command from,  from key word
  • Example:

    123456789

    // In SQL

    sql> select * from Product Note: Product is the table name right....!!!

    // In HQL

    hql> select p from Product p [ or ] from Product p Note: here p is the reference...!!

     

  • If we want to load the Partial Object from the database that is only selective properties (selected columns) of an objects then we need to replace column names with POJO class variable names.
  • Example:

    123456789101112

    // In SQL

    sql> select pid,pname from Product Note: pid, pname are the columns Product is the table name right..!

    // In HQL

    hql> select p.productid,p.productName from Product p [ or ] from Product p ( we should not start from, from key word here because we selecting the columns hope you are getting me ) Note: here p is the reference...!! productid,productName are POJO variables

     

  • It is also possible to load or select the object from the database by passing run time values into the query,  in this case we can use either ” ? ” symbol or label in an HQL command, the index number of  ” ? ” will starts from zero but not one ( Remember this, little important regarding interview point of view)
  • Example:

    1234567891011121314

    // In SQL

    sql> select * from Product where pid=? Note: Product is the table name right..!

    // In HQL

    hql> select p from Product p where p.productid=? [ or ] select p from Product p where p.productid=:java4s [ or ] from Product p where p.productid=? [ or ] from Product p where p.productid=:java4s Note: Here p is the reference...!!
  • Here :java4s refers that, java4s is a label(colon ‘:’ symbol represents that its the label)
  • we used to store some values in this label in run time (in our class)

Related Articles

post a comment