Wiki source code of EOF-Using EOF-Fetching
Last modified by David Avendasora on 2010/09/11 23:52
Hide last authors
| author | version | line-number | content |
|---|---|---|---|
| |
2.1 | 1 | == Overview == |
| 2 | |||
| 3 | There are many ways to specify what Entities to fetch using EOF. | ||
| 4 | |||
| 5 | 1. Model a Fetch Specification in EntityModeler | ||
| |
12.1 | 6 | 1. Create a FetchSpecification in code |
| |
2.1 | 7 | 1. Use EOUtilities helper methods |
| |
12.1 | 8 | 1. User helper fetch methods in your Entity.java (if generated using a more advanced EOGenerator* template). |
| |
2.1 | 9 | |
| |
12.1 | 10 | *EOGenerator, Velocity EOGenerator, JavaEOGenerator |
| |
2.1 | 11 | |
| |
12.1 | 12 | For complete details please read the [[EOF Documentation>>url:http://developer.apple.com/documentation/WebObjects/Enterprise_Objects/About/chapter_1_section_1.html||shape="rect"]], the rest of this article will give some simple examples of the different kinds of fetches. |
| |
2.1 | 13 | |
| 14 | == Examples == | ||
| 15 | |||
| 16 | === Model a Fetch Specification in EntityModeler === | ||
| 17 | |||
| 18 | ==== Create the Fetch Specfication ==== | ||
| 19 | |||
| 20 | ~1. Open your model in EntityModeler and select an Entity to define a fetch spec for. | ||
| |
12.1 | 21 | 2. Click on the "New Fetch Specification" tool bar button. |
| 22 | 3. In the Properties tab, give the new Fetch Specification a name. ie: userForLoginCredentials | ||
| 23 | 4. Enter a qualifier string in the text area provided. ie: | ||
| |
2.1 | 24 | |
| |
12.1 | 25 | {{code}} |
| 26 | (username = $username) and (password = $password) | ||
| 27 | {{/code}} | ||
| 28 | |||
| |
2.1 | 29 | Binding variables (eg: $username) will be replaced by user supplied values when the Fetch Specification is called. |
| 30 | |||
| 31 | ==== Fetch using a Modeled Fetch Specification ==== | ||
| 32 | |||
| 33 | {{code}} | ||
| 34 | |||
| 35 | String username; //Assume exists | ||
| 36 | String password; //Assume exists | ||
| 37 | EOEditingContext editingContext; //Assume exists | ||
| 38 | NSMutableDictionary bindings = new NSMutableDictionary(); | ||
| 39 | bindings.takeValueForKey(username, Person.USERNAME_KEY); | ||
| 40 | bindings.takeValueForKey(password, Person.PASSWORD_KEY); | ||
| 41 | String fetchSpecName = "userForLoginCredentials"; | ||
| 42 | String entityName = Person.ENTITY_NAME; | ||
| 43 | NSArray objects = EOUtilities.objectsWithFetchSpecificationAndBindings(editingContext, entityName, fetchSpecName, bindings); | ||
| 44 | |||
| 45 | {{/code}} | ||
| 46 | |||
| 47 | === Create a Fetch Specification in code === | ||
| 48 | |||
| 49 | ==== Build a simple qualifier ==== | ||
| 50 | |||
| 51 | {{code}} | ||
| 52 | EOKeyValueQualifier qualifier = new EOKeyValueQualifier(keyName, EOQualifier.QualifierOperatorEqual, value); | ||
| 53 | {{/code}} | ||
| 54 | |||
| 55 | ==== Build a more complex qualifier ==== | ||
| 56 | |||
| 57 | {{code}} | ||
| 58 | |||
| 59 | EOKeyValueQualifier qual1 = new EOKeyValueQualifier(keyName1, EOQualifier.QualifierOperatorEqual, value1); | ||
| 60 | EOKeyValueQualifier qual2 = new EOKeyValueQualifier(keyName2, EOQualifier.QualifierOperatorEqual, value2); | ||
| |
8.1 | 61 | EOAndQualifier finalQual = new EOAndQualifier(new NSArray(new object[] {qual1, qual2})); |
| |
2.1 | 62 | |
| 63 | {{/code}} | ||
| 64 | |||
| 65 | ==== Build a Fetch Specification ==== | ||
| 66 | |||
| 67 | {{code}} | ||
| 68 | |||
| 69 | NSArray sortOrderings; // Assume Exists (or null for no defined sort) | ||
| 70 | EOQualifier qualifier; // Assume Exists (or null for all objects) | ||
| 71 | EOFetchSpecification fetchSpec = new EOFetchSpecification("EntityName", qualifier, sortOrderings); | ||
| 72 | |||
| 73 | {{/code}} | ||
| 74 | |||
| 75 | ==== Fetch with the Fetch Specification ==== | ||
| 76 | |||
| 77 | {{code}} | ||
| 78 | |||
| 79 | String username; //Assume exists | ||
| 80 | String password; //Assume exists | ||
| 81 | EOEditingContext editingContext; //Assume exists | ||
| 82 | EOKeyValueQualifier usernameQual = new EOKeyValueQualifier(Person.USERNAME_KEY, EOQualifier.QualifierOperatorEqual, username); | ||
| 83 | EOKeyValueQualifier passwordQual = new EOKeyValueQualifier(Person.PASSWORD_KEY, EOQualifier.QualifierOperatorEqual, password); | ||
| |
8.1 | 84 | EOAndQualifier credentialsQual = new EOAndQualifier(new NSArray(new object[] {usernameQual, passwordQual})); |
| |
2.1 | 85 | EOFetchSpecification userForCredentialsFetchSpec = new EOFetchSpecification(Person.ENTITY_NAME, credentialsQual, null); |
| 86 | NSArray fetchedObjects = editingContext.objectsWithFetchSpecification(userForCredentialsFetchSpec); | ||
| 87 | |||
| 88 | {{/code}} | ||
| 89 | |||
| 90 | === EOUtilities === | ||
| 91 | |||
| |
12.1 | 92 | [[EOUtilities>>url:http://developer.apple.com/documentation/WebObjects/Reference/WO53_Reference/com/webobjects/eoaccess/EOUtilities.html||shape="rect"]] has several helper methods for simplifying your fetchs, we've already used "objectsWithFetchSpecificationAndBindings" above. |
| |
2.1 | 93 | |
| 94 | Also look at: | ||
| 95 | |||
| |
14.1 | 96 | {{code}}String username; //Assume exists |
| |
2.1 | 97 | EOEditingContext editingContext; //Assume exists |
| |
14.1 | 98 | EOEnterpriseObject eo = EOUtilities.objectMatchingKeyAndValue(editingContext, Person.ENTITY_NAME, Person.USERNAME_KEY, username);{{/code}} |
| |
2.1 | 99 | |
| |
14.1 | 100 | {{code}}EOEditingContext editingContext; //Assume exists |
| |
2.1 | 101 | NSDictionary bindings; //Assume exists |
| |
14.1 | 102 | EOEnterpriseObject eo = EOUtilities.objectMatchingValues(editingContext, Person.ENTITY_NAME, bindings);{{/code}} |
| |
2.1 | 103 | |
| |
14.1 | 104 | {{code}}EOEditingContext editingContext; //Assume exists |
| 105 | NSArray objects = EOUtilities.objectsForEntityNamed(editingContext, Person.ENTITY_NAME);{{/code}} | ||
| |
2.1 | 106 | |
| |
14.1 | 107 | {{code}}String username; //Assume exists |
| |
2.1 | 108 | EOEditingContext editingContext; //Assume exist |
| |
14.1 | 109 | NSArray objects = EOUtilities.objectsMatchingKeyAndValue(editingContext, Person.ENTITY_NAME, Person.USERNAME_KEY, username);{{/code}} |
| |
2.1 | 110 | |
| 111 | {{code}} | ||
| 112 | |||
| 113 | EOEditingContext editingContext; //Assume exists | ||
| 114 | NSDictionary bindings; //Assume exists | ||
| 115 | NSArray objects = EOUtilities.objectsMatchingValues(editingContext, Person.ENTITY_NAME, bindings); | ||
| 116 | |||
| 117 | {{/code}} | ||
| 118 | |||
| 119 | === EOGenerator Template Helper Methods. === | ||
| 120 | |||
| |
14.1 | 121 | Depending on your EOGenerator [[templates>>doc:WOL.Home.EOGenerator.EOGenerator Templates and Additions.WebHome]] you may have auto generated methods in your _Entity.java class that will make fetching objects easier. Look for methods like: |
| |
2.1 | 122 | |
| |
14.1 | 123 | {{code}}EOEditingContext editingContext; //Assume exists |
| 124 | NSArray people Person.fetchAllPeople(editingContext);{{/code}} | ||
| |
2.1 | 125 | |
| 126 | {{code}} | ||
| 127 | |||
| 128 | EOEditingContext editingContext; //Assume exists | ||
| 129 | EOQualifier qualifer; //Assume exists | ||
| 130 | NSArray people Person.fetchPeople(editingContext, qualifier); | ||
| 131 | |||
| 132 | {{/code}} |