Hibernate Query Language

In the previous issue of Javajazzup you learned about Hibernate Query Language and its different kind of clauses. Lets quickly focus on the overview of HQL.

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. HQL has its own object-oriented query language and supports native SQL. It automatically generates the sql query and executes it against underlying database.

Hibernate Query Language is extremely powerful and it supports Polymorphism, Associations, Much less verbose than SQL. It uses Classes and properties instead of tables and columns.

Hibernate uses the following ways to retrieve objects from the database:

• Hibernate Query Language (HQL)
• Query By Criteria (QBC) and Query BY Example (QBE) using Criteria API
• Native SQL queries

Lets discuss about the Hibernate Criteria APIs in brief.

Introduction to Hibernate Criteria APIs Hibernate Criteria API is a powerful and elegant alternative to traditional HQL. It is mostly used in case of complex multi criteria search screens, where HQL is not very effective. It provides a well-designed way of building dynamic queries on Hibernate-persisted databases.

The interfaces of the Criteria Query API represent an aspect of the relational approach. There are five core APIs that are commonly used. These are:

1. Criteria
2. Criterion
3. Restrictions
4. Projection
5. Order


1. Criteria

The interface org.hibernate.Criteria is used to create the criterion for the search. It is a simplified API for retrieving entities by composing Criterion objects. This is a very convenient approach for functionality like “search” screens where there is a number of conditions or fields
to be placed upon the result set.

Criteria Interface provides the following methods:

Method Description
add(Criterion criterion) Adds a Criterion to
constrain the
results to be
addOrder(Order order) Add an Order to the
result set.

String alias)

Join an association , assigning an alias
to the joined entity
This method is used to create new
Criteria, “rooted” at the associated
setFetchSize(int fetchSize) Set a fetch size for the underlying JDBC query.
setFirstResult(int first Result) Set the first result to be retrieved.
setMaxResults(int maxResults) Set a limit upon the number of objects
to be retrieved.
uniqueResult() Convenience method to return a single instance that matches the query, or null if the query returns no results.


March 2008 | Java Jazz Up | 45
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

, 59,

Download PDF