6.2 Finding a Set of Objects Using an Extent


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.

graphics/06fig02.jpg

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.



Core Java Data Objects
Core Java Data Objects
ISBN: 0131407317
EAN: 2147483647
Year: 2003
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net