Re: update object in Object Store
Re: update object in Object Store
- Subject: Re: update object in Object Store
- From: "Jerry W. Walker" <email@hidden>
- Date: Fri, 16 Mar 2007 14:41:07 -0400
Hi, Sean,
In direct answer to your question, if you've named your editing
context "ec", do an:
ec.saveChanges();
...after you've done the field updates that you've described.
If you're new to WO, be sure to read over the EOF Commandments to
avoid some pernicious problems:
http://en.wikibooks.org/wiki/Programming:WebObjects/EOF/Using_EOF/
The_EOF_Commandments
If any of the commandments aren't obvious to you, post another
question to the list.
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 relationshiop 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.
I hope this helps.
Regards,
Jerry
On Mar 16, 2007, at 1:49 PM, Sean wrote:
I'm pretty new to WebObjects and I'm trying update a record in my
User Table.
To insert the record I used insertObject(user), but how do i do an
update on user?
for example
user.setName("Mickey Mouse");
user.setPhone("212-555-1111");
-Sean
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40gmail.com
This email sent to email@hidden
--
__ Jerry W. Walker,
WebObjects Developer/Instructor for High Performance Industrial
Strength Internet Enabled Systems
email@hidden
203 278-4085 office
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden