Newsletter

Part 1 Hibernate Query Language Introduction

Hibernate » on Jul 8, 2011 { 9 Comments } By Sivateja

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 Inheritance, polymorphism, Associations(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:

// 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:

// 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:

// 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)

 Next Session HQL part 2

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
- Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

9 Responses to “Part 1 Hibernate Query Language Introduction”
  1. bhanu says:

    when to use hibernate criteria query and HQL. Please suggest…

  2. Mai Hung says:

    Hello, java4s team.
    I have a doubt about this advantage.

    “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 ”

    May you explan me or give me a example about this advantage, please?

    Thank you.

    • Niteen says:

      Hi…i think you might know that for different databases we need to write different different queries…but if you r using hibernate then you can write queries in HQL and these queries are common for all the databases…Like Oracle,MySQL,SQL….

  3. Sai Krishna says:

    Awesome website to learn from scratch….

  4. Shivani says:

    Best website i ever have seen 🙂

  5. Archita patnaik says:

    Thank you Java4s group , for clearing concept from scratch..its really helpful
    Keep rocking…Best website ever ever…

  6. Vijay Jagtap says:

    Thank U for Devloped this site.
    it is very usefull to student who are fresher in Hibernate.

  7. Atul says:

    Thank u for writing tutorial…

    Best website …

  8. Swati Rupnar says:

    Good explanation and easily understandable…Thanks for sharing this tutorial

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2018 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.