Earlier in Chapters 3 and 5, we discussed the javax.jdo.Extent interface. The Extent interface offers a convenient programmatic way to retrieve a collection of similar objects or an object hierarchy from the underlying datastore. Recall that an Extent like the one shown in Figure 6-2 represents a collection of all instances in the datastore of a particular persistence-capable class. An Extent can consist of just instances of the single class or can also include instances of subclasses. By default, it is possible to get an Extent for any persistence-capable class. Figure 6-2. The javax.jdo.Extent interface.
An application obtains an Extent for a class from a PersistenceManager and then uses the iterator() method to go through all instances using a java.util.Iterator . The following code snippet shown again from ReadByExtentExample.java in Chapter 3 demonstrates the use of an Extent in finding instances of a particular class: tx.begin(); Extent extent = pm.getExtent(Author.class, true); Iterator results = extent.iterator(); while (results.hasNext()) { Author author = (Author) results.next(); String name = author.getName(); System.out.println("Author's name is '" + name + "'."); } extent.closeAll(); tx.commit(); Note that when used alone in the manner shown above, the Iterator can potentially include lots of objects because it returns all the instances of Author and the subclasses. The Extent interface offers a convenient mechanism to locate persistent instances of a particular persistence-capable class in the datastore; however, most applications would need a more specific mechanism to locate instances that match specific criteria. |