Updated July 10, 2023
What is HQL?
HQL is an abbreviation for hibernate query language. Hibernate is a platform to connect the traditional databases to object-oriented language (specifically JAVA). It is a query language in hibernate is similar to SQL in traditional RDBMS except for the fact that we use an entity in HQL instead of tables. It is written embedded in JAVA code and various functions from the JAVA library are used to convert HQL to SQL. It can be called as an object-oriented language embossed with SQL query statements. It is a flexible and user-friendly language having its own syntax and grammar to retrieve, store, update information from the database. It reduces the impedance mismatch between JAVA and RDBMS.
Why do we need HQL?
As the importance of JAVA as a language for platforms like the internet is increasing, we find more it relevant to connect our JAVA based application to back end with the help of hibernate. Hibernate uses HQL language instead to establish the connection between database and front-end.
We need HQL when we want to select some particular fields and columns as per our requirements. Methods adopted earlier were not efficient enough to drill down to this level for example fetching the result set or data set from the database as a whole record having the number of rows and columns. This approach does not give the flexibility to narrow down the search and makes the application heavy and sluggish. This approach is used by JDBC Connectors, asp.net, and many more languages. Using HQL reduces this time gap and provides specific results. Hence It is more relevant to be used in a real-time environment where JAVA is involved in the front end.
How HQL Works?
HQL is an XML file format to link java from the front end to the database in the back end. The SQL queries which we fire in the database directly using sql queries can be written in hql as well. The HQL has its own syntax where we can write the query and then that query is converted into SQL statements which can be understood by the database. This is written in java language to reduce the impedance mismatch.
HQL is a case-insensitive language except for the name of classes and entities. For example: org.hibernate.eg.test is not equal to org.hibernate.eg.Test since the “test” and “Test” are two different entities in HQL.
Advantages of HQL
There are several advantages of HQL as a language:
- The coder does not have any obligation to learn the SQL language.
- HQL is object-oriented and its performance is good when we link our front-end application to the backend.
- HQL has caching memory and thereby improves speed.
- HQL supports popular features of OOPs concepts like polymorphism, inheritance, and association.
Syntax along with HQL Query Examples
Some simple queries in hibernate look like:
Using FROM clause:
From eg.Test or From Test.
This statement will return all instances of the class. In this case, it is Test. We can also create an alias for eg: From Test as a test. Here “test” is the alias of Test. This alias can then be used later instead of class.
Example #1
Code:
String hqlquery = "FROM Test";
Query q = session.createQuery(hqlquery);
List display = q.list();
AS Clause: From eg.Test AS T or From Test AS T.
This statement is used when we want to create aliases to the main classes of HQL. This is a useful technique in case we have long queries. We can simply assign the query to the alias and then use that alias for further data handling. Aliasing can be done without the AS keyword as well. For ex: From Test T.
Example #2
Code:
String hqlquery = "FROM Test AS T";
Query q = session.createQuery(hqlquery);
List display = q.list();
WHERE Clause: From eg.Test T WHERE T.code=102 or From Test T WHERE T.code=102.
This clause is used when we are searching a particular data in the database table. So here if we are searching for a particular record on the basis of test code which we have then this clause is used in the query. This will help in narrowing down the searching criterion. If we give primary key f the table in where clause then we should see the significant improvement in searching speed.
Example #3
Code:
String hqlquery = "FROM Test T WHERE T.code = 102";
Query q = session.createQuery(hqlquery);
List display = q.list();
SELECT Clause:
From eg. SELECT T.number FROM Test T.
This clause is used if we want to select a particular column from the database table. This is one of the ways of narrow down the searching criterion. Whatever field name we give in the select clause only that will be selected. It is useful to fetch a small amount of data if we have specific information on the same.
Example #4
Code:
String hql = "SELECT E.firstName FROM Employee E";
Query query = session.createQuery(hql);
List results = query.list();
DELETE Clause:
String hqlexample = "DELETE FROM Test " +
"WHERE code = : test_code";
This clause in the query can be used to delete one or more objects from the connected database table. Both of the “transient” and “persistent” objects can be deleted b this way. This is the simple query to delete any number of fields or tables altogether from the database. This should be used carefully.
Example #5
Code:
String hqlquery = "DELETE FROM Test " +
"WHERE code = : test_code";
Query q = session.createQuery(hqlquery);
q.setParameter("test_code", 102);
int display = q.executeUpdate();
System.out.println("Hence the number of rows modified are: " + display);
Conclusion
Hence HQL is an elegant object-oriented language that bridges the gap between object-oriented JAVA and database management system. With the highest market share hibernate query language is becoming a popular language to work on.
Recommended Articles
This is a guide to HQL. Here we discuss why do we need HQLwith the Advantages, Working, and Syntax along with Query Examples. You may also have a look at the following articles to learn more –