Red Hat Web Application Framework 6.1 Manuale Utente

Pagina di 230
Chapter 9. Persistence Tutorial
83
query retrieveAllUsers {
User myUser;
do {
select user_id, first_name, last_name from users
} map {
user.id = users.user_id;
user.firstName = users.first_name;
user.lastName = users.last_name;
}
}
DataQuery query = session.retrieveQuery("retrieveAllUsers");
// notice how the attribute name corresponds directly to what
// in the map" section of the query and is actully
// the

object type

.

attribute name

query.addEqualsFilter("user.firstName", fName);
while (query.next()) {
DataObject user = query.get("user");
System.out.println("First name = " + user.get("firstName") +
"; Last name = " + user.get("lastName") +
"; Group = " + user.get("groupName"));
}
9.5.1.4. Restricting the number of rows returned
One common feature that is requested of queries, collections, and associations is to be able to restrict
the number of rows that are returned. Specifically, in order to create any sort of pagination or to break
up a large set of results in to a series of smaller sets it is necessary to restrict the number of rows
returned.
Restricting the number of rows a query returns is easy. To do so, you can simple call
setRange(Integer, Integer)
on the data query is question. For instance, if I want results 10-19
for a qury, I can do the following:
DataQuery query = session.retrieveQuery("retrieveAllUsers");
query.setRange(new Integer(10), new Integer(20));
while (query.next()) {
DataObject user = query.get("user");
System.out.println("First name = " + user.get("firstName") +
"; Last name = " + user.get("lastName") +
"; Group = " + user.get("groupName"));
}
9.5.1.5. Filtering Using Subqueries
The filtering methods described so far handle most situations. However, sometimes developers need
the ability to filter based on the results of a subquery. Therefore, the persistence layer provides a
mechanism to allow developers to use named queries within filters. Specifically, this is useful within
IN
clauses and
EXISTS
clauses.
Suppose that you want to retrieve all articles written by authors whose last name begins with the letter
"b". One easy way to avoid duplicates is to use an
IN
clause. To perform this operation, you can create
the following two DataQueries:
query retrieveArticlesBasedOnAuthor {
BigDecimal authorID;
do {