Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Accessing Database Keys

One of the great benefits of the Enterprise Objects frameworks is that they insulate you from the complexities of relational databases. It does this in part by managing things like primary and foreign keys for you, automatically.

However, while developing applications, you may need to access a particular entity’s primary and foreign keys for debugging and other purposes. There are a number of facilities within the frameworks that help you do this. The recommended way is to use a fetch specification to fetch certain rows of data and set that fetch specification to fetch raw rows rather than enterprise objects. The results of the fetch return an array of dictionaries; each dictionary represents one row of data and includes keys for all of an entity’s attributes, whether or not they are class properties.

For example, consider the Listing entity in the Real Estate model. Perhaps you need to determine the primary key (the listingID attribute) of all the listings in the database and the foreign key for the agent who is responsible for each listing (the agentID attribute). Neither of these properties are class properties in the Listing entity, so invoking the key-value coding method valueForKey("listingID") or valueForKey("agentID") on a Listing enterprise object results in an exception. However, if you construct a fetch specification and set it to fetch raw rows, you can easily retrieve the values for each of these keys.

The code in “Listing 6-2” provides an example of fetching all Listing records as raw rows and extracting the listingID primary key and the agentID foreign key.

Listing 5-2  Fetch Listing records as raw rows

EOFetchSpecification fs = new EOFetchSpecification("Listing", null, null);
    fs.setFetchesRawRows(true);
    NSArray rawRows = editingContext.objectsWithFetchSpecification(fs,         editingContext);
 
    java.util.Enumeration enum = rawRows.objectEnumerator();
    while (enum.hasMoreElements()) {
      NSDictionary row = (NSDictionary)enum.nextElement();
      if (row != null) {
        NSLog.out.appendln("\nlistingID pk: " + row.valueForKey("listingID"));
        NSLog.out.appendln("\nagentID fk: " + row.valueForKey("agentID"));
      }
    }


< Previous PageNext Page > Hide TOC


Last updated: 2007-07-11




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice