Java Reference
In-Depth Information
bb
+ " ORDER BY DELIVERY_TIME desc";
bb
SQLQuery query = session
bb
.createSQLQuery(sqlQuery);
bb
query.addEntity("o", Order.class);
bb
query.addJoin("r", "o.restaurant");
bb
query.setParameter("name", "Oakland");
bb
List results = query.list();
bb
return results;
bb
}
}
…
}
Notice that the
SELECT
statement uses placeholders such as {o.*} for the column
names. Hibernate replaces them with the real column names when it executes the
SELECT
statement. The call to
SQLQuery.addEntity()
tells Hibernate that the table
alias
o
refers to the
Order
class, and the call to
SQLQuery.addJoin()
tells Hibernate
that the table alias
r
refers to the order's
restaurant
property. The
list()
method returns a list of arrays. Each array consists of an
Order
and its
Restaurant
.
The restaurant is also accessible by calling
Order.getRestaurant()
. If your appli-
cation needs to navigate the result set, it can also execute Hibernate
SQL
queries
by calling
scroll()
.
SQL
queries enable an application to execute
SQL
statements while staying
within the Hibernate framework. However, they have some drawbacks:
1
You must generate queries by concatenating
SQL
fragments, which are
messy and difficult to maintain.
2
Using
SQL
directly embeds some knowledge of the database and the
schema in the application code.
3
The query must retrieve all the properties of the objects, which might be
inefficient.
Therefore, although
SQL
queries are useful if you need to manipulate the persistent
objects, a better approach is to use i
BATIS
to generate and execute the queries. It
requires less code and gives you more control over how the result set is processed.
11.7 Summary
Designing and implementing a search screen that lets the user search for entities
that match certain search criteria is challenging. The application must implement
a paging mechanism in order to handle result sets that are too large to load entirely
into memory or display on a single page. It must generate queries dynamically,
Search WWH ::
Custom Search