WebObjects/Database Development

The WO (EOF, actually) way of handling rows (objects) can be confusing if you are using to thinking in SQL. The general outline of using WO for database rows is:

FETCHING AN INITIAL SET OF OBJECTS
To fetch objects from your persistence layer (retrieve records from the database which will be converted to EnterpriseObjects on the fly), do the following:

- create your EOModel or obtain one that fits

- declare the NSArray in which you want store the Enterprise Objects that your table's rows will be turned into

- establish your editing context, "ec" (for simple cases, just use WOSession.defaultEditingContext, otherwise, be sure to lock/unlock).

- create an EOQualifier (if you want to filter the objects coming from a given table)

- create a set of EOSortOrderings (if you want your records presorted when they are retrieved)

- create an EOFetchSpecification, fs, to tie the fetch together

- execute ec.objectsWithFetchSpecification(fs);

OBTAINING RELATED OBJECTS
To obtain any object that is related (has an EORelationship) to any of the EnterpriseObjects that you've already fetched, just refer to them as you would to any object referenced by another POJO (Plain Old Java Object). The reference will be enough to cause WO to "fire a fault" which causes the referenced objects to be retrieved for you automatically.

UPDATING OBJECTS
If you've done enough to fetch objects from your database, then to update records, just update the appropriate fields (best to use the get/set methods) of the object and do:

ec.saveChanges;

CREATING NEW OBJECTS
Although there are a few ways to do this, the safest and probably most efficient is to use:

EOUtilities.createAndInsertInstance(ec, "myNewEntitiesName"); ec.saveChanges;

DELETING OBJECTS
In order to delete an object, first you must explicitly delete it from the ec then save the changes:

ec.deleteObject(myObject); ec.saveChanges;

RELATING OBJECTS
If you have two EnterpriseObject classes that have an EORelationship, such as a one to many relationship for instance as follows:

A <-->> B

or

objectA className myBs ( -->>B )

objectB name myA ( -->A )

then to create two such objects and relate them to one another, you would:

myA = (A)EOUtilities.createAndInsertInstance(ec, "A"); myB = (B)EOUtilities.createAndInsertInstance(ec, "B"); myA.addObjectToBothSidesOfRelationshipWithKey(b, "myBs"); // set other attributes of both myA and myB here. ec.saveChanges;

You could also, of course, have used:

myB.addObjectToBothSidesOfRelationshipWithKey(a, "myA");

The important thing to remember is that the addObjectToBothSidesOfRelationshipWithKey method need only be used once on one of the objects to establish the relationship in both directions whether either or both of the relationships is a toOne or a toMany and whether the relationship is bidirectional or not. It's a VERY HELPFUL method.

Also see removeObjectFromBothSidesOfRelationshipWithKey for it's deleting counterpart.

NOTES ON THE EDITING CONTEXT
In general, remember that the purpose of an editing context is to provide a runtime context for your program in which each EnterpriseObject is unique (within the editing context) and in which you can create, add, update and delete objects made known to that editing context. However, in general, nothing you do within your editing context is reflected in the database until you do ec.saveChanges. Then, all the changes you've made since the last saveChanges will be committed to the database.

There are esoteric exceptions to the above, but that's the best way to remember it.

There are, of course, many other things to know about WebObjects, but knowing the above should help you a lot and at least provide you with hooks to look up many of the ideas in the API reference.

A Warning
If you're new to WO, be sure to read over the EOF Commandments to avoid some pernicious problems.

If any of the commandments aren't obvious to you, ask someone.