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
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
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:
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
||Adds a Criterion to
results to be
||Add an Order to the
|Join an association , assigning an alias
to the joined entity
|This method is used to create new
Criteria, “rooted” at the associated
||Set a fetch size for the underlying JDBC query.
|setFirstResult(int first Result)
||Set the first result to be retrieved.
||Set a limit upon the number of objects
to be retrieved.
||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