Open In App

HQL | Introduction

Introduction 

Hibernate Query Language (HQL) is an easy to learn and powerful query language designed as an object-oriented extension to SQL that bridges the gap between the object-oriented systems and relational databases. The HQL syntax is very similar to the SQL syntax. it has a rich and powerful object-oriented query language available with the Hibernate ORM, which is a technique of mapping the objects to the databases. The data from object-oriented systems are mapped to relational databases with a SQL-based schema. 

HQL can also be used to retrieve objects from database through O/R mapping by performing the following tasks: 

The Hibernate Query Language, designed as a “minimal” object-oriented extension to SQL provides an elegant bridge between Java classes and database tables. Hence it is mandatory to understand the need of HQL in current scenario.

Features of HQL 

HQL queries are case insensitive; however, the names of Java classes and properties are case-sensitive HQL is used to execute queries against database. If HQL is used in an application to define a query for a database, the Hibernate framework automatically generates the SQL query and executes it. Unlike SQL, HQL uses classes and properties in lieu of tables and columns. HQL supports polymorphism as well as associations, which in turn allows developers to write queries using less code as compared to SQL. In addition, HQL supports many other SQL statement and aggregate functions, such as sum() and max() and clauses, such as group by and order by.

Need of HQL 

It is more beneficial to use HQL instead of native SQ retrieve data from databases. The following are some of the reasons why HQL is preferred over SQL:

HQL Syntax

HQL is considered to be the most powerful query language designed as a minimal object-oriented extension to SQL. HQL queries are easy to understand and use persistent class and property names, instead of table and column names. HQL consists of the following elements.

HQL queries

A query within a query is known as a subquery and is surrounded by parenthesis. An example of a subquery is subselect in which a select clause is embedded in another clause, such as select, from, and where clause. Subquery is executed before the execution of the main query.

For Example, The following query returns items where all bids are less than 100.

from Item  where 100>all (Select b.amount from item.bids b)

To retrieve the items with bids greater than 100, you can use the following query:

from Item  where 100<all (Select b.amount from item.bids b)

How to Run HQL

After developing the servlets, JSP pages, and configuration files, you should store them at the appropriate location in the HibernateApplication directory structure. Create a Web Archive (WAR) file and name it as HibernateApplication.war.Deploy the HibernateApplication.war file on the GlassFish V3 application server. After a successful deployment of HibernateApplication, Perform the following steps to run the application: Open the Internet Explorer web browser and type the following URL:

http://localhost:8080/HibernateApplication/Myservlet
 

Article Tags :
SQL