Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
8Activity
0 of .
Results for:
No results containing your search query
P. 1
Hibernate Tutorial 09

Hibernate Tutorial 09

Ratings: (0)|Views: 985|Likes:
Published by api-27399718

More info:

Published by: api-27399718 on Oct 15, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

Page 1 of 8
Hibernate Tutorial 09 Hibernate Query Language
By Gary Mak
gary.teaching@gmail.com
September 2006
1. Querying objects

When using JDBC to access databases, we write SQL statements for the query and update tasks. In such case, we are dealing with tables, columns and joins. When using Hibernate, most update tasks can be finished through the APIs provided by Hibernate. However, using a query language for the query tasks is still necessary. Hibernate is providing a powerful query language called Hibernate query language (HQL).

HQL is database independent and translated into SQL by Hibernate at runtime. When writing HQL, we can concentrate on the objects and properties without knowing much detail on the underlying database. We can treat HQL as an object-oriented variant of SQL.

In the previous chapters, we have already experienced some basic HQL statements for querying objects. For example, we can use the following HQL to query for all books, and then call the list() method to retrieve the result list which containing book objects.

Query query = session.createQuery("from Book");
List books = query.list();

The Query interface provides two methods for retrieving only a subset of the results, ranged by the offset (which is zero-based) and record count. They are very useful for displaying the results in a table with multiple pages.

Query query = session.createQuery("from Book");
query.setFirstResult(20);
query.setMaxResults(10);
List books = query.list();
Another query attribute that will have performance impact is the fetch size. It tells the underlying
JDBC driver how many rows should be transferred for a single request.
Query query = session.createQuery("from Book");
query.setFetchSize(100);
List books = query.list();

When using HQL, we can specify query parameters at the same way as we do for SQL queries. If we are sure that there will be only one unique object returned as result, we can call the uniqueResult() method to retrieve it. Null will be returned if nothing matched.

Page 2 of 8

Query query = session.createQuery("from Book where isbn = ?");
query.setString(0, "1932394419");
Book book = (Book) query.uniqueResult();

In the example above, we use \u201c?\u201d to represent a query parameter and set it by index, which is zero-based not one-based as in JDBC. This kind of parameters is called \u201cPositional Parameters\u201d. We can also use \u201cNamed Parameters\u201d for our queries. The advantages of using named parameters are easy to understand and able to occur for multiple times.

Query query = session.createQuery("from Book where isbn =:isbn");
query.setString("isbn", "1932394419");
Book book = (Book) query.uniqueResult();

In this tutorial, we will introduce more details about HQL. It is also beneficial to monitor the SQL
statements generated for writing high performance queries.
2. The from clause

Now let\u2019s begin with the from clause of a HQL statement. It is the only necessary part of a HQL statement. The following HQL statement is used for querying the books whose name contains the word \u201cHibernate\u201d. Notice that the \u201cname\u201d is a property of Book but not a database column.

from Book
where name = 'Hibernate Quickly'

Or you can assign an alias for the object. It\u2019s useful when you are querying multiple objects in one query. We should use the naming conventions for classes and instances in Java. Notice that the \u201cas\u201d keyword is optional.

from Book as book
wherebook.name = 'Hibernate Quickly'

We can specify more than one class in the from clause. In such case, the result will contain a list of Object[]. For the following statement, it will be a \u201ccross join\u201d of book objects and publisher objects since there\u2019s not any where clauses.

from Book book, Publisher publisher
3. Joining associations

In HQL, we can use the \u201cjoin\u201d keyword to join our associated objects. The following query finds all the books published by the publisher \u201cManning\u201d. The result contains a list of object pairs in the form of Object[]. Each pair consists of a book object and a publisher object.

Page 3 of 8
from Book book join book.publisher publisher
where publisher.name = 'Manning'

In addition to many-to-one associations, all other kinds of associations can also be joined. For example, we can join the one-to-many association from book to chapters as well. The following query finds all the books containing a chapter \u201cHibernate Basics\u201d. The result contains a list of object pairs also. Each pair consists of a book object and a collection of chapters.

from Book book join book.chapters chapter
where chapter.title = 'Hibernate Basics'
3.1. Implicit Joins

In the above joins, we specify a keyword \u201cjoin\u201d for joining associated objects. This kind of joins is called \u201cexplicit joins\u201d. In fact, we can reference an association by its name directly. This will cause an \u201cimplicit joins\u201d. For example, the above two queries can be expressed as follows. The result will contain a list of book objects only since no join is specified in the from clause.

from Book book
where book.publisher.name = 'Manning'
from Book book
where book.chapters.title = 'Hibernate Basics'
For a collection association, an implicit join occurs each time when it is navigated. That means if
we navigate the same collection for two times, the same table will be joined for two times also.
from Book book
where book.chapters.title = 'Hibernate Basics' and book.chapters.numOfPages = 25
So we must be careful when using implicit joins with collection association. For the collection to be
referenced more than one time, we should use \u201cexplicit join\u201d to avoid duplicated joins.
from Book book join book.chapters chapter
where chapter.title = 'Hibernate Basics' and chapter.numOfPages = 25
3.2. Joining types

If we use the following HQL to query for books joining with publishers, we will find that the books with null publisher will not be included. This type of joins is called \u201cinner join\u201d and it is default for joins if we don\u2019t specify any join type or specify as \u201cinner join\u201d. It has the same meaning as the inner join in SQL.

from Book book join book.publisher

Activity (8)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
Girish Chaubey liked this
k2sh liked this
t0ilatung liked this
jow11 liked this
xkou liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->