Why Hibernate Query Language(HQL)?

Hibernate Query Language:
Hibernate Query Language (HQL) is same as SQL (Structured Query Language).Hibernate created a new language named Hibernate Query Language (HQL), the syntax is quite similar to database SQL language. The main difference between is HQL uses class name instead of table name, and property names instead of column name.

Advantage of HQL:-
1) database independent:
2) HQL fully supports polymorphic queries. That is, along with the object to be returned as a query result, all child objects (objects of subclasses) of the given object shall be returned.
3) HQL is suitable for executing Static Queries.
4) HQL doesn’t support pagination concept.
5) HQL is to perform both select and non-select operations on the data.

Clauses in the HQL are:
• from
• select
• where
• order by
• group by
FROM Clause:-
• String hql = “FROM Employee”;
• Query query = session.createQuery(hql);
• List results = query.list();
SELECT Clause:
• String hql = “SELECT E.firstName FROM Employee E”;
• Query query = session.createQuery(hql);
• List results = query.list();

WHERE Clause:
• String hql = “FROM Employee E WHERE E.id = 10”;
• Query query = session.createQuery(hql);
• List results = query.list();

ORDER BY Clause:

• String hql = “FROM Employee E WHERE E.id > 10 ORDER BY E.salary DESC”;
• Query query = session.createQuery(hql);
• List results = query.list();

GROUP BY Clause:
• String hql = “SELECT SUM(E.salary), E.firtName FROM Employee E ” +
• “GROUP BY E.firstName”;
• Query query = session.createQuery(hql);
• List results = query.list();

Aggregate functions are:
• avg(…), sum(…), min(…), max(…)
• count(*)
• count(…), count(distinct …), count(all…)

HQL Select Query Example:-
Retrieve a stock data where stock code is “7211”.
Query query = session.createQuery(“from Stock where stockCode = :value “);
query.setParameter(“value”, “7211”);
List list = query.list();
Query query = session.createQuery(“from Stock where stockCode = ‘7211’ “);
List list = query.list();
HQL Update Query Example:-

Query query = session.createQuery(“update Stock set stockName = :stockName” +” where stockCode = :stockCode”);
query.setParameter(“stockName”, “DIALOG11”);
query.setParameter(“stockCode”, “7211”);
int result = query.executeUpdate();
Query query = session.createQuery(“update Stock set stockName = ‘DIALOG2′” +” where stockCode = ‘7211’”);
int result = query.executeUpdate();

HQL Delete Query Example:-

Query query = session.createQuery(“delete Stock where stockCode = :stockCode”);
query.setParameter(“stockCode”, “7211”);
int result = query.executeUpdate();
Query query = session.createQuery(“delete Stock where stockCode = ‘7211’”);
int result = query.executeUpdate();

HQL Insert Query Example:-
In HQL, only the INSERT INTO … SELECT … is supported; there is no INSERT INTO … VALUES. HQL only support insert from another table. For example:

1) “insert into Object (id, name) select oo.id, oo.name from OtherObject oo”;
Insert a stock record from another backup_stock table. This can also called bulk-insert statement:
Query query = session.createQuery(“insert into Stock(stock_code, stock_name)” +
“select stock_code, stock_name from backup_stock”);
int result = query.executeUpdate();

You may also like to read:
Hibernate Mapping file
Hibernate Configuration file

Jar file creation

Advantages of Hibernate Framework

Traditional way of storing objects in a database table, is to break up object into primitive data types, and store each primitive data in a table column. Application developer need to spend more time on writing mapping source code between class data members and table columns,etc… Situation worsens when relationship exists between objects, which need to be stored in database.

Hibernate provides solution for above problems.

Below is anatomy of Java Application, which uses Hibernate Framework.

hibernate-advantages
hibernate-advantages

1.Reduces size of code, hence development of Application is faster, which in turn controls cost of overall project.
When Hibernate is used, you need not write code to perform below routine activities
Connecting to database, retrieve individual columns from database table and creating object out of it , and vice versa.

2.Transaction Control: Hibernate has built in Transaction Management. A Transaction can be started, commited or rolled back by using Hibernate.
3.Automatic creation and updation of tables or columns. Required tables and columns are automatically created or updated, by provding hbm2ddl property in hbm file. Hibernate gets details of table names, column names, type,etc… from Hibernate Mapping file(i..e hbm.xml)

4.Provides HQL, Hibernate Query language, using which developer need to provide queries in terms of class names, and data members, instead of focusing on tables and columns.

5.Hibernate Caching, caches objects, so that when same object(s) are queried next, object from cache is returned, and database hit is avoided. This improves overall performance of Application.

In you may find, few program statements used again and again, like openSession(), beginTransaction(), commit(), rollBack(), close(),etc…which can be avoided multiple times, by better designing your application. Another alternative to further reduce Hibernate code, is to use combination of Spring and Hibernate.

Other facts:
Hibernate Framework further depends on JDBC, to interact with underlying database. Hence JDBC driver jar file need to be added to projects using Hibernate.

Such Framework is referred as ORM(Object Relational Mapping)

You may also like to read:
Mapping Type in HIbernate Mapping file
DDL with JDBC Statments
How many types of JDBC Drivers exist