Child pages
  • EOF-Using EOF-Fetching
Skip to end of metadata
Go to start of metadata

Overview

There are many ways to specify what Entities to fetch using EOF.

  1. Model a Fetch Specification in EntityModeler
  2. Create a FetchSpecification in code
  3. Use EOUtilities helper methods
  4. User helper fetch methods in your Entity.java (if generated using a more advanced EOGenerator* template).

*EOGenerator, Velocity EOGenerator, JavaEOGenerator

For complete details please read the EOF Documentation, the rest of this article will give some simple examples of the different kinds of fetches.

Examples

Model a Fetch Specification in EntityModeler

Create the Fetch Specfication

1. Open your model in EntityModeler and select an Entity to define a fetch spec for.
2. Click on the "New Fetch Specification" tool bar button.
3. In the Properties tab, give the new Fetch Specification a name. ie: userForLoginCredentials
4. Enter a qualifier string in the text area provided. ie:

(username = $username) and (password = $password)

Binding variables (eg: $username) will be replaced by user supplied values when the Fetch Specification is called.

Fetch using a Modeled Fetch Specification

String username; //Assume exists
String password; //Assume exists
EOEditingContext editingContext; //Assume exists
NSMutableDictionary bindings = new NSMutableDictionary();
bindings.takeValueForKey(username, Person.USERNAME_KEY);
bindings.takeValueForKey(password, Person.PASSWORD_KEY);
String fetchSpecName = "userForLoginCredentials";
String entityName = Person.ENTITY_NAME;
NSArray objects = EOUtilities.objectsWithFetchSpecificationAndBindings(editingContext, entityName, fetchSpecName, bindings);

Create a Fetch Specification in code

Build a simple qualifier

EOKeyValueQualifier qualifier = new EOKeyValueQualifier(keyName, EOQualifier.QualifierOperatorEqual, value);

Build a more complex qualifier

EOKeyValueQualifier qual1 = new EOKeyValueQualifier(keyName1, EOQualifier.QualifierOperatorEqual, value1);
EOKeyValueQualifier qual2 = new EOKeyValueQualifier(keyName2, EOQualifier.QualifierOperatorEqual, value2);
EOAndQualifier finalQual = new EOAndQualifier(new NSArray(new object[] {qual1, qual2}));

Build a Fetch Specification

NSArray sortOrderings; // Assume Exists (or null for no defined sort)
EOQualifier qualifier; // Assume Exists (or null for all objects)
EOFetchSpecification fetchSpec = new EOFetchSpecification("EntityName", qualifier, sortOrderings);

Fetch with the Fetch Specification

String username; //Assume exists
String password; //Assume exists
EOEditingContext editingContext; //Assume exists
EOKeyValueQualifier usernameQual = new EOKeyValueQualifier(Person.USERNAME_KEY, EOQualifier.QualifierOperatorEqual, username);
EOKeyValueQualifier passwordQual = new EOKeyValueQualifier(Person.PASSWORD_KEY, EOQualifier.QualifierOperatorEqual, password);
EOAndQualifier credentialsQual = new EOAndQualifier(new NSArray(new object[] {usernameQual, passwordQual}));
EOFetchSpecification userForCredentialsFetchSpec = new EOFetchSpecification(Person.ENTITY_NAME, credentialsQual, null);
NSArray fetchedObjects = editingContext.objectsWithFetchSpecification(userForCredentialsFetchSpec);

EOUtilities

EOUtilities has several helper methods for simplifying your fetchs, we've already used "objectsWithFetchSpecificationAndBindings" above.

Also look at:

String username; //Assume exists
EOEditingContext editingContext; //Assume exists
EOEnterpriseObject eo = EOUtilities.objectMatchingKeyAndValue(editingContext, Person.ENTITY_NAME, Person.USERNAME_KEY, username);
EOEditingContext editingContext; //Assume exists
NSDictionary bindings; //Assume exists
EOEnterpriseObject eo = EOUtilities.objectMatchingValues(editingContext, Person.ENTITY_NAME, bindings);
EOEditingContext editingContext; //Assume exists
NSArray objects = EOUtilities.objectsForEntityNamed(editingContext, Person.ENTITY_NAME);
String username; //Assume exists
EOEditingContext editingContext; //Assume exist
NSArray objects = EOUtilities.objectsMatchingKeyAndValue(editingContext, Person.ENTITY_NAME, Person.USERNAME_KEY, username);
EOEditingContext editingContext; //Assume exists
NSDictionary bindings; //Assume exists
NSArray objects = EOUtilities.objectsMatchingValues(editingContext, Person.ENTITY_NAME, bindings);

EOGenerator Template Helper Methods.

Depending on your EOGenerator templates you may have auto generated methods in your _Entity.java class that will make fetching objects easier. Look for methods like:

EOEditingContext editingContext; //Assume exists
NSArray people Person.fetchAllPeople(editingContext);
EOEditingContext editingContext; //Assume exists
EOQualifier qualifer; //Assume exists
NSArray people Person.fetchPeople(editingContext, qualifier);
  • No labels