PATH  WebObjects 4.0 Documentation > WebObjects Programming Topics

Prefetching

Synopsis

Describes how to fetch all related objects at once.

Discussion

When the Enterprise Objects Framework fetches an object, it creates EOFaults for the object's relationships. Each time you access one of these EOFaults, EOF fetches the data from the database. This procedure works well and minimizes the amount of unneeded data fetching from the database. However, each time an EOFault is accessed, a separate database query is performed. Using a technique called "batch faulting" can reduce the number of database queries. If you know that you will be faulting all instances of a particular relationship, you can use a technique called "prefetching" to skip the EOFault creating stage and produce a single database query to load all related objects.

By using EOFetchSpecification's setPrefetchingRelationshipKeyPaths to specify the key paths of relationships, the Enterprise Objects Framework uses the qualifier of the fetch specification along with the related attributes to perform a joined query against the related objects' table, thereby causing all the appropriate objects to be loaded from the database at once.

An Example

A Movie has a to-one relationship to a Studio. Assume that you have a WOComponent that lets you query movies and display all the movies with their studios. Without using prefetching, every time the component displays a group of Movies, the Enterprise Objects Framework makes a separate query to the database to get each Studio. You can use prefetching to ensure that every time the movies are fetched, all the appropriate studios are also fetched. This technique can significantly reduce the number of queries against a database.

How to Use Prefetching

Following are two different ways to enable batch faulting:

When to Use Prefetching

You can use prefetching when you know that a particular relationship will always be accessed. It is more instructive, however, to know when not to use prefetching:

See Also

Questions

Keywords

Revision History

2 July, 1998. Paul Haddad. First Draft.

17 November, 1998. Terry Donoghue. Second Draft.