Java Reference
In-Depth Information
the relationship and whether to load it eagerly or lazily. For example, here is a
criteria query that finds orders and loads their restaurants eagerly:
public class HibernateOrderRepositoryImpl … {
public PagedQueryResult findOrders(int startingIndex,
List orders = session
.createCriteria(Order.class)
.add(Expression.eq("deliveryAddress.city", "San Francisco"))
.setFetchMode("restaurant",FetchMode.JOIN)
.list()
}
}
Hibernate will execute this query using a SELECT statement that does a join
between the PLACED_ORDER and RESTAURANT tables.
In addition to eagerly loading the related objects, another way to optimize object
loading is to load only those object fields that are required. Let's take a look.
11.3.3
Loading a subset of an object's fields
Search screens typically display a subset of each object's fields and require the
user to navigate to a details screen to see the rest. For example, as figure 11.1
shows, the View Orders screen only displays the order's number, phone number,
email, delivery time and status, and the restaurant's name. We can rely on lazy
loading to prevent the query from unnecessarily loading related objects such as
the order's line items or the restaurant's menu items. But, by default, JDO and
Hibernate will load all of an object's attributes and all of the attributes of its
embedded objects.
This means, for example, that the persistence framework will execute a SELECT
statement that retrieves the order's delivery address, which is an embedded
object, even though it is never displayed. The PLACED_ORDER table only has a few
columns; the overhead of loading them all is insignificant. But if a table has many
columns or has columns containing large values, then loading only the required
objects can often improve performance. Not only does this approach reduce the
amount of data that is transferred over the network from the database to the
application, but it also reduces the amount of processing that the persistence
framework must do to instantiate the Java objects.
As we have seen in chapters 4, 5, and 6, JDO and Hibernate provide various mech-
anisms for controlling which fields are loaded. The three mechanisms that are par-
ticularly useful when implementing dynamic paged queries are JDO fetch groups,
 
Search WWH ::




Custom Search