HQL | 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:
- Apply restrictions to properties of objects
- Arrange the results returned by a query by using the order by clause
- Paginate the results
- Aggregate the records by using group by and having clauses
- Use Joins
- Create user-defined functions
- Execute subqueries
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:
- Provides full support for relational operations. It is possible to represent SQL Queries in the form of objects in HQL which uses classes and properties instead of tables and columns.
- Return results as objects. In other words, the query results are in the form of objects rather than the plain text. These objects can be used to manipulate or retrieve data in an application. This eliminates the need of explicitly creating objects and populating the data from the resultset retrieved from the execution of a query.
- Supports polymorphic queries. Polymorphic queries return the query results along with all the child objects (objects of subclasses), if any.
- Easy to learn and use. The syntax of HQL is quite similar to that of SQL This makes Hibernate queries easy to learn and implement in the applications.
- Supports many advanced features as compared to SQL, such as pagination, fetch join with dynamic profiling (initialization the associations or collection of values with their parent objects can be done using a single select statement), inner/outer/full joins, and cartesian product. It is also supports projections, aggregation (max, avg), grouping, ordering, sub queries, and SQL function calls.
- Provides database independency. HQL helps in writing database independent queries that are converted into the native SQL syntax of the database at runtime. This approach helps to use the additional features that the native SQL query provides.
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.
- Clauses (FROM, Select, Where, Order by, Group by)
- Associations and joins (Inner join, Left outer join, Right outer join, Full join)
- Aggregate functions (avg, sum, min, max, count, etc.)
- Expressions (Mathematical operators, Binary comparison, String concatenation, SQL scalar function, etc.)
- Subqueries (any, all, some, in)
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 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 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:
Attention reader! Don’t stop learning now. Learn SQL for interviews using SQL Course by GeeksforGeeks.