Software Developer
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.
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:
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.
String hqlString = "From techiioData";
Query hqlQuery = session.createQuery(hqlString);
List resultSet = hqlQuery.list();
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.
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();
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.
String hqlString = "SELECT ed.nameOfTechnology,ed.toolType FROM techiioData ed";
Query hqlQuery = session.createQuery(hqlString);
List resultSet = hqlQuery.list();
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.
String hqlString = "SELECT ed.nameOfTechnology FROM educbaData ed WHERE ed.toolType='Language'";
Query hqlQuery = session.createQuery(hqlString);
List resultSet = hqlQuery.list();
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.
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.
String hqlString = "FROM educbaData ed ORDER BY ed.sessions DESC,ed.nameOfTechnology";
Query hqlQuery = session.createQuery(hqlString);
List resultSet = hqlQuery.list();
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.
String hqlString = "SELECT ed.toolType, SUM(ed.sessions) FROM educbaData ed GROUP BY ed.toolType";
Query hqlQuery = session.createQuery(hqlString);
List resultSet = hqlQuery.list();
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.
String hqlString = "FROM educbaData ed WHERE ed.session = :sessionCount";
Query hqlQuery = session.createQuery(hqlString);
hqlQuery.setParameter("sessionCount",10);
List resultSet = hqlQuery.list();
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.
String hqlString = "UPDATE educbaData SET sessions=35 WHERE nameOfTechnology = 'Javascript'";
Query hqlQuery = session.createQuery(hqlString);
int affectedRowCount = hqlQuery.executeUpdate();
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.
String hqlString = "DELETE FROM educbaData WHERE session < :minSessionCount";
Query hqlQuery = session.createQuery(hqlString);
hqlQuery.setParameter("minSessionCount",5);
int affectedRowCount = hqlQuery.executeUpdate();
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.
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();