Previous | Home | Next |
It is an object based version of SQL, it is used by hibernate to fetch data. Hibernate even provide a criterion API that provide a type safe and object oriented way to retrieve object from database.Using SQL has following shortcoming:
- SQL is standarized but it is vendor -dependent features.
- SQL is designed more specifically to work with relational database tables but not object.
To overcome these issues Hibernate introduce its own object oriented query language called Hibernate Query Language(HQL).
Advantages Of Using HQL
- HQL queries are database independent.
- HQL provide a support for ordering the result persist objects.
- HQL is more object oriented which makes us write more easily than Sql.
- HQL support pagination.
We know that the Hibernate is a query based language which is work on objects. So we can say that the hibernate is the object oriented language. It works on the table and column. We can say that it works with the persistent objects and their properties. In Hibernate we translate the SQL Query which works to perform the action on the database.
We can use SQL statements directly with hibernate using Native SQL but we always use the HQL Query with database. It gives us the advantes of Hibernate SQL generation and caching strategies. We always remember that in SQL SELECT,FROM,WHERE keyword are not case sensitive but properties like table and column names are case sensitive in HQL. We divided into some basic category to the SQL properties which we use. So here we will discuss that how we need to use?
We use FROM when we want to fetch table from the database. In hibernate we can say that we use this when we want to load a complete persistent objects into memory.
With the help of the following syntex we can understand what is the mean of tha above line:
String hql = "FROM Hibernate_HQL"; Query query = session.createQuery(hql); List results = query.list(); When we use a class thorough a package then we need to fully qualify a class name in HQL, just specify the package and class name as follows: String hql = "FROM com.hibernatebook.criteria.Hibernate_HQL"; Query query = session.createQuery(hql); List results = query.list();
The AS performed common access in SQL. But in the HQL it can be used to assign aliases to the classes in HQL queries, it uses when you have long queries. For instance, our previous simple example would be the following:
String hql = "FROM Hibernate_HQL AS HQ"; Query query = session.createQuery(hql); List results = query.list(); The AS keyword is optional and you can also specify the alias directly after the class name, as follows: String hql = "FROM Hibernate_HQL HQ"; Query query = session.createQuery(hql); List results = query.list();
The SELECT clause provides more control over the result set than the from clause. If you want to obtain few properties of objects instead of the complete object, use the SELECT clause. Following is the simple syntax of using SELECT clause to get just first_name field of the Employee object:
String hql = "SELECT HQ.firstName FROM Hibernate_HQL HQ"; Query query = session.createQuery(hql); List results = query.list();
It is notable here that Hibernate_HQL.firstName is a property of Hibernate_HQL object rather than a field of the Hibernate_HQL table.
If you want to narrow the specific objects that are returned from storage, you use the WHERE clause.
Following is the simple syntax of using WHERE clause:
String hql = "FROM Hibernate_HQL HQ WHERE HQ.id = 10"; Query query = session.createQuery(hql); List results = query.list();
To sort your HQL query's results, you will need to use the ORDER BY clause. You can order the results by any property on the objects in the result set either ascending (ASC) or descending (DESC). Following is the simple syntax of using ORDER BY clause:
String hql = "FROM Hibernate_HQL HQ WHERE HQ.id > 10 ORDER BY HQ.salary DESC"; Query query = session.createQuery(hql); List results = query.list();
If you wanted to sort by more than one property, you would just add the additional properties to the end of the order by clause, separated by commas as follows:
String hql = "FROM Hibernate_HQL HQ WHERE HQ.id > 10 " + "ORDER BY HQ.firstName DESC, HQ.salary DESC "; Query query = session.createQuery(hql); List results = query.list();