论坛: 菜鸟乐园 标题: 高手帮忙翻译下HibernateinAction中的部分内容 复制本贴地址    
作者: ndbf123 [ndbf123]    论坛用户   登录
从第7.1到7.2.
7.1 Executing queries
The Query and Criteria interfaces both define several methods for controlling
execution of a query. In addition, Query provides methods for binding concrete values
to query parameters. To execute a query in your application, you need to
obtain an instance of one of these interfaces, using the Session.
7.1.1 The query interfaces
To create a new Query instance, call either createQuery() or createSQLQuery().
The createQuery() method prepares an HQL query:
Query hqlQuery = session.createQuery("from User");
The createSQLQuery() is used to create a SQL query, using the native syntax of the
underlying database:
Query sqlQuery = session.createSQLQuery(
"select {u.*} from USERS {u}", "u",
User.class
);
In both cases, Hibernate returns a newly instantiated Query object that may be
used to specify exactly how a particular query should be executed, and to execute
the query.
To obtain a Criteria instance, call createCriteria(), passing the class of the
objects you want the query to return. This is also called the root entity of the criteria
query, the User in this example:
Criteria crit = session.createCriteria(User.class);
The Criteria instance may be used in the same way as a Query object―but it’s also
used to construct the object-oriented representation of the query by adding Criterion
instances and navigating associations to new Criterias. We discuss this in
much more detail later; for now, let’s continue with query execution.
Paging the result
Pagination is a commonly used technique. Users might see the result of their search
request (for example, for specific Items) as a page. This page shows only a limited
subset (say, 10 Items) at a time, and users can navigate to the next and previous
pages manually. Both the Query and Criteria interfaces support this pagination of
the query result:
Query query =
session.createQuery("from User u order by u.name asc");
query.setFirstResult(0);
query.setMaxResults(10);
The call to setMaxResults(10) limits the query result set to the first 10 objects
selected by the database. In this criteria query, the requested page starts in the
“middle” of the result set:
Criteria crit = session.createCriteria(User.class);
crit.addOrder( Order.asc("name") );
crit.setFirstResult(40);
crit.setMaxResults(20);
List results = crit.list();
Starting from the fortieth object, we retrieve the next 20 objects. Note that there is
no standard way to express pagination in SQL―but Hibernate knows the tricks to
make this work efficiently on your particular database.
You can use the method-chaining coding style (methods return the receiving
object instead of void) with both the Query and Criteria interfaces, rewriting the
two previous examples as follows:
List results =
session.createQuery("from User u order by u.name asc")
.setFirstResult(0)
.setMaxResults(10)
.list();
List results =
session.createCriteria(User.class)
.addOrder( Order.asc("name") )
.setFirstResult(40)
.setMaxResults(20)
.list();
Chaining method calls is less verbose and is supported by many Hibernate APIs.
Listing and iterating results
The list() method executes the query and returns the results as a list:
List result = session.createQuery("from User").list();
With some queries, we know the result will be only a single instance―for example,
if we want only the highest bid. In this case, we can read it from the result list by
index: result.get(0) or setMaxResult(1). We then execute the query with the
uniqueResult() method, because we know only one object will be returned:
Executing queries 245
Bid maxBid =
(Bid) session.createQuery("from Bid b order by b.amount desc")
.setMaxResults(1)
.uniqueResult();
Bid bid = (Bid) session.createCriteria(Bid.class)
.add( Expression.eq("id", id) )
.uniqueResult();
If the query returns more than one object, an exception will be thrown.
The Query and Session interfaces also provide the iterate() method, which
returns the same result as list() (or find()) but uses a different strategy for
retrieving the results. When you use iterate() to execute a query, Hibernate
retrieves only the primary key (identifier) values in the first SQL select; it tries to
find the rest of the state of the objects in the cache, before querying again for the
rest of the property values. This technique can be used to optimize loading in specific
cases, as discussed in section 7.6, “Optimizing object retrieval.”
FAQ Is Session.find() faster than Query.list()? The Session API provides
shortcut methods for simple queries. Instead of creating a Query, you can
also call Session.find("from User"). The result is the same as from
Query.list(); one isn’t faster than the other. The same is true for iterate():
You’re free to choose the API. However, it’s highly likely that the
query shortcut methods on the Session API will be removed in the future
to reduce the bloat of session methods. We recommend the Query API.
Finally, the Query interface lets you bind values to query parameters.
谢谢了

地主 发表时间: 07-03-03 23:49

回复: an_tao_1 [an_tao_1]   论坛用户   登录
在线英语翻译工具

B1层 发表时间: 07-03-05 09:16

论坛: 菜鸟乐园

20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon

粤ICP备05087286号