Introduction to Hibernate Query Language
Hibernate Query Language or HQL for short is
extremely powerful query language. HQL is much
like SQL and are case-insensitive, except for
the names of the Java Classes and properties.
Hibernate Query Language is used to execute
queries against database. Hibernate
automatically generates the sql query and
execute it against underlying database if HQL
is used in the application. HQL is based on the relational object models and makes the SQL
object oriented. Hibernate Query Language uses
Classes and properties instead of tables and
columns. Hibernate Query Language is
extremely powerful and it supports
Polymorphism, Associations, Much less verbose
There are other options that can be used while
using Hibernate. These are Query By Criteria
(QBC) and Query BY Example (QBE) using
Criteria API and the Native SQL queries
- Full support for relational
operations: HQL allows representing
SQL queries in the form of objects.
Hibernate Query Language uses
Classes and properties instead of
tables and columns.
- Return result as Object: The HQL
queries return the query result(s) in
the form of object(s), which is easy
to use. These eliminate the need of
creating the object and populate the
data from result set.
- Polymorphic Queries: HQL fully
supports polymorphic queries.
Polymorphic queries results the query
results along with all the child objects
- Easy to Learn: Hibernate Queries are
easy to learn and it can be easily
implemented in the applications.
- Support for Advance features: HQL
contains many advance features such
- as pagination, fetch join with dynamic
profiling, Inner/outer/full joins,
Cartesian products. It also supports
Projection, Aggregation (max, avg)
and grouping, Ordering, Sub queries
and SQL function calls.
- Database independent: Queries
written in HQL are database
independent (If database supports
the underlying feature).
Understanding HQL Syntax
Any Hibernate Query Language may consist of
- Aggregate functions
Clauses in the HQL are:
- order by
- group by
Aggregate functions are:
- avg(...), sum(...), min(...), max(...)
- count(...), count(distinct ...),
Subqueries are nothing but a query within
another query. Hibernate supports Subqueries
if the underlying database supports it.
Lets understand, how these HQL queries are
implemented in a POJO based class.
Preparing table for HQL Examples
Create insurance table and populate it with
the data. To create the insurance table and
insert the sample data, run the following sql
2008 | Java Jazz Up |31
|View All Topics
|All Pages of this Issue