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