HQL(Hibernate Query Language) method of session interface

HQL(Hibernate Query Language) method of session interface
Techiio-author
Written by Sagar RabidasFebruary 17, 2022
11 min read
Hibernate
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss Hibernate Query Language.

Introduction of Hibernate Query Language:-

Hibernate question language is an object-orientated query language that resembles dependent question language (sq.) however in contrast to square, it works on chronic objects as opposed to tables. HQL also permits you to jot down square queries with the assist of local sq. However, we have to generally try and write HQL queries because it has many blessings. Database independence makes database portability simpler and extra green. Aside from this it's miles very easy to analyze for java builders and supports polymorphic queries and supports caching at numerous degrees.

HQL internally converts the question into a square which then, in turn, performs operations on the table contents. Therefore, it's far called the mediator or from i.e object-relational mapping device which acts as the bridge between gadgets and tables within the relational database. Help is case insensitive in terms of its key phrases or clauses but is case sensitive while bringing up item names or continual entities of tables.

Method of Session Interface:-

Query objects can be obtained by using the createQuery() method of the session interface. The Query interface of hibernating provides us with many methods like follows:

  1. public int executeUpdate(): We can perform manipulations like updating and deletion.
  2. public List list(): It is used while retrieving the result from a particular query.
  3. public Query setFirstResult(int row): It helps in specifying starting from which row number we want to retrieve the records.
  4. public Query set a result(int record count): It helps in specifying the number of the rows we want to retrieve.
  5. public Query setParameter(int position, Object value): It helps in specifying the value of the JDBC styled parameter of the query.
  6. public Query setParameter(String name, Object value): It helps in specifying the value of the named parameter of the query.

FROM Clause:-

When you need to retrieve the complete object into the reminiscence, we can use the from clause to bring up the call of the item that you need to retrieve. As an example, there may be one desk named techiio_data which shops the name of technology, period, sessions, tool type.

Code:

String hqlString = "From techiioData";
Query hqlQuery = session.createQuery(hqlString);
List resultSet = hqlQuery.list();

AS Clause:-

It is used to specify the alternative name or alias to the entity calling. For instance, in the above instance, we can deliver an alias to our entity techiio data as ed inside the following way.

Code:

String hqlString = "FROM educbaData AS ed";
//OR can also be used without AS "FROM techiioData ed";
Query hqlQuery = session.createQuery(hqlString);
List resultSet = hqlQuery.list();

SELECT Clause:-

While we need to retrieve unique houses of entities or column values of the table then we use the pick out clause to specify so. Think, we simplest want to get the call of the era and tool type from desk techiio_data then we will write an HQL question for the same inside the following way.

Code:

String hqlString = "SELECT ed.nameOfTechnology,ed.toolType FROM techiioData ed";
Query hqlQuery = session.createQuery(hqlString);
List resultSet = hqlQuery.list();

WHERE Clause:-

When we have to get data satisfying some specific conditions, we can narrow up the result set using the WHERE clause which helps us specify the condition clauses that particular properties should satisfy. Suppose we want to get only those technology names who have language in the column tool type column using the HQL query. Here is how we do it.

Code:

String hqlString = "SELECT ed.nameOfTechnology FROM educbaData ed WHERE ed.toolType='Language'";
Query hqlQuery = session.createQuery(hqlString);
List resultSet = hqlQuery.list();

ORDER BY Clause:-

If we need to get them taken care of information, we can specify the column call and ascending or descending order. We also can type primarily based on more than one column in the HQL query by specifying the comma-separated property names. As an example, arranging the facts in ascending order primarily based on the name of generation may be carried out in the following manner.

Code:

String hqlString = "FROM educbaData ed ORDER BY ed.nameOfTechnology";
Query hqlQuery = session.createQuery(hqlString);
List resultSet = hqlQuery.list();

By default, sorting is in ascending order. Now suppose if we want the data to be sorted based on the number of sessions in descending order and then on the name of technology in ascending order which can be done in the following way.

Code:

String hqlString = "FROM educbaData ed ORDER BY ed.sessions DESC,ed.nameOfTechnology";
Query hqlQuery = session.createQuery(hqlString);
List resultSet = hqlQuery.list();

GROUP BY Clause:-

When we want to group the result based on some of the column foundation then we can use the organization by using clauses. It's miles most often used alongside the aggregate functions to acquire summarized grouped values of certain columns/houses. Assume you need to get the full number of classes grouped based totally on the tool type field in HQL. We will do so in the following way.

Code:

String hqlString = "SELECT ed.toolType, SUM(ed.sessions) FROM educbaData ed GROUP BY ed.toolType";
Query hqlQuery = session.createQuery(hqlString);
List resultSet = hqlQuery.list();

Using Named Parameters:-

Many times, we face a situation where we need to mention the values of the fields at runtime or externally. We can do so by using named parameters. We can get all the records with 10 sessions in the following way.

Code:

String hqlString = "FROM educbaData ed WHERE ed.session = :sessionCount";
Query hqlQuery = session.createQuery(hqlString);
hqlQuery.setParameter("sessionCount",10);
List resultSet = hqlQuery.list();

Updating the records:-

We can update one or greater information on the use of the HQL question using the replace clause. We can update the consultation remember of the “JavaScript” name of the era’s session count number to 35 using the following HQL question. Execute update method is used for replacing and deletion which returns the number of rows that are affected whilst appearing manipulation.

Code:

String hqlString = "UPDATE educbaData SET sessions=35 WHERE nameOfTechnology = 'Javascript'";
Query hqlQuery = session.createQuery(hqlString);
int affectedRowCount = hqlQuery.executeUpdate();

Deleting the records:-

We can delete the information by way of the use of the delete clause in the HQL question. If we need to delete the records whose session count is much less than five then we can achieve this by using the usage of the execute update approach within the following way.

Code:

String hqlString = "DELETE FROM educbaData WHERE session < :minSessionCount";
Query hqlQuery = session.createQuery(hqlString);
hqlQuery.setParameter("minSessionCount",5);
int affectedRowCount = hqlQuery.executeUpdate();

Inserting the records:-

We will insert the data inside the following way in HQL. As an alternative, we can use the equal approach of the session interface and bypass the object of the entity containing our preferred values.

Code:

String hqlString = "INSERT INTO educbaData(nameOfTechnology, duration, sessions, toolType) SELECT nameOfTechnology, duration, sessions, toolType FROM edducationalPlatforms WHERE nameOfTechnology='Hibernate'";
Query hqlQuery = session.createQuery(hqlString);
int affectedRowCount = hqlQuery.executeUpdate();

Hibernate
Hibernate Query
Query Language
Java
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Sagar Rabidas
Software Developer
Techiio-followerTechiio-followerTechiio-followerTechiio-followerTechiio-follower
+8 more
300 Blog Posts
14 Discussion Threads
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance