InsertObject
Moin list, Could someone answer the following question? : I have two objects A and B, each extending EOGenericRecord. Object A has a field of type B. Now, when I create a new Object A which embeds a new object B, before performing an EditingContext.saveChanges I must insert each object separately in my editingContext: myContext.insertObject(B); myContext.insertObject(A); myContext.saveChanges(); which is rapidly over-complicated when saving complex object-graphs Now, in order to perform the saveChanges in a more elegant way, can someone tell me if there is a method to override, method called by EditingContext.saveChanges, in which I could in each EOGenericRecord do the insertObject work ? Thanks Fj ___ Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list (Webobjects-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: InsertObject
EOUtilities.createAndInsertInstance(anEditingContext, entityName) Am 30.05.2007 um 10:59 schrieb Frederic JECKER: Moin list, Could someone answer the following question? : I have two objects A and B, each extending EOGenericRecord. Object A has a field of type B. Now, when I create a new Object A which embeds a new object B, before performing an EditingContext.saveChanges What does 'embed' mean here? A relationship? I must insert each object separately in my editingContext: myContext.insertObject(B); myContext.insertObject(A); myContext.saveChanges(); which is rapidly over-complicated when saving complex object- graphs You insert them when you create them, not later. That does not seem complex to me!? Now, in order to perform the saveChanges in a more elegant way, can someone tell me if there is a method to override, method called by EditingContext.saveChanges, in which I could in each EOGenericRecord do the insertObject work ? 1. That would be too late. 2. How should the EditingContext know about Objects that are not already in it or vice versa? 3. You can not connect Objects by relationships that are not inside the same EditingContext. Regards, atze ___ Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list (Webobjects-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: InsertObject
Frederic, I'm not sure what you mean by over-complicated. Whenever you create an EO, you need to insert it into the editing context - you should do this immediately when the object is created. You can then perform a save changes at any time, and it will serialize all outstanding changes to the database. It's easiest to think of saveChanges() as more like a committing a set of changes, rather than 'saving' an object. You could make thousands of changes to your EOs, then at the end, when the object graph is complete, just call saveChanges() on the editing context. There is no reason to call saveChanges() multiple times... in fact, if you do, you could end up with a database that is out of sync. If you make all your object graph changes, then save at the end, you'll either save everything to to the database, or nothing. Ken On May 30, 2007, at 4:59 AM, Frederic JECKER wrote: Moin list, Could someone answer the following question? : I have two objects A and B, each extending EOGenericRecord. Object A has a field of type B. Now, when I create a new Object A which embeds a new object B, before performing an EditingContext.saveChanges I must insert each object separately in my editingContext: myContext.insertObject(B); myContext.insertObject(A); myContext.saveChanges(); which is rapidly over-complicated when saving complex object- graphs Now, in order to perform the saveChanges in a more elegant way, can someone tell me if there is a method to override, method called by EditingContext.saveChanges, in which I could in each EOGenericRecord do the insertObject work ? Thanks Fj ___ Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list (Webobjects-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/webobjects-dev/kenlists% 40anderhome.com This email sent to [EMAIL PROTECTED] ___ Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list (Webobjects-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: InsertObject
Hi, In my case, I can't insert objects in the editing context at their creation but only at the end of the transaction because the user can cancel his action at any time. Doing as decribed works (I do it myself) but if I do so, I will need to call at editingContext.revert() to prevent unwanted changes to be saved in the database by another part of my app using the same context. To sum up, I'm just looking for a method like EOEditingContext.awakeFromInsert except that it would be called before inserting objects instead of after. This way I could insert relationship members in the editingContext for each EOCustomObject in their own classes and avoid huge code blocks of insert in my main controller Regards FJ ___ Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list (Webobjects-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: InsertObject
Don't fight it (you'll lose). Create an editingContext for each user, put the objects into that editingContext as soon as you create them, revert the editingContext if you decide not to save their changes. Alan On May 30, 2007, at 5:20 AM, Frederic JECKER wrote: Hi, In my case, I can't insert objects in the editing context at their creation but only at the end of the transaction because the user can cancel his action at any time. Doing as decribed works (I do it myself) but if I do so, I will need to call at editingContext.revert() to prevent unwanted changes to be saved in the database by another part of my app using the same context. To sum up, I'm just looking for a method like EOEditingContext.awakeFromInsert except that it would be called before inserting objects instead of after. This way I could insert relationship members in the editingContext for each EOCustomObject in their own classes and avoid huge code blocks of insert in my main controller Regards FJ ___ Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list (Webobjects-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/webobjects-dev/award% 40apple.com This email sent to [EMAIL PROTECTED] ___ Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list (Webobjects-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: InsertObject
On 30-May-07, at 7:20 AM, Frederic JECKER wrote: Hi, In my case, I can't insert objects in the editing context at their creation but only at the end of the transaction because the user can cancel his action at any time. Doing as decribed works (I do it myself) but if I do so, I will need to call at editingContext.revert() to prevent unwanted changes to be saved in the database by another part of my app using the same context. To sum up, I'm just looking for a method like EOEditingContext.awakeFromInsert except that it would be called before inserting objects instead of after. This way I could insert relationship members in the editingContext for each EOCustomObject in their own classes and avoid huge code blocks of insert in my main controller Frederic, You need to rethink your apps structure as this *will* lead to trouble and it breaks the EOF commandments. http://en.wikibooks.org/wiki/Programming:WebObjects/EOF/Using_EOF/ The_EOF_Commandments Don't bother arguing that it works for you :-), we've been down this path many times, check the list archives. The correct solution is to create a peer or child EOEditingContext to wrap a potentially disposable action so your users can walk away without penalty and *always* insert your EOs right away. -- ;david -- David LeBer Codeferous Software 'co-def-er-ous' adj. Literally 'code-bearing' site: http://codeferous.com blog: http://davidleber.net profile: http://www.linkedin.com/in/davidleber -- Toronto Area Cocoa / WebObjects developers group: http://tacow.org ___ Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list (Webobjects-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: InsertObject
Am 30.05.2007 um 13:20 schrieb Frederic JECKER: Hi, In my case, I can't insert objects in the editing context at their creation but only at the end of the transaction because the user can cancel his action at any time. That is why we use EditingContexts create EdCon new insert new insert user cancels - just throw EdCon away user commits - EdCon.saveChanges() Doing as decribed works (I do it myself) but if I do so, I will need to call at editingContext.revert() No. Just use a new EdCon. to prevent unwanted changes to be saved in the database by another part of my app using the same context. Don't use the same. To sum up, I'm just looking for a method like EOEditingContext.awakeFromInsert except that it would be called before inserting objects instead of after. This way I could insert relationship members in the editingContext for each EOCustomObject in their own classes and avoid huge code blocks of insert in my main controller You are using EOF the wrong way if you try that. Just don’t. And yes, multiple EdCons are the normal way to work with this. Thats why every WOSession has one for itself. So that you don't have to think about the other Sessions' EDCons. atze ___ Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list (Webobjects-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: InsertObject
As Alan pointed out, you could revert the editing context. Other options are to just dispose that EC, or use the undo manager to undo any changes. Trust us - this is the right approach. Ken On May 30, 2007, at 7:36 AM, Alan Ward wrote: Don't fight it (you'll lose). Create an editingContext for each user, put the objects into that editingContext as soon as you create them, revert the editingContext if you decide not to save their changes. Alan On May 30, 2007, at 5:20 AM, Frederic JECKER wrote: Hi, In my case, I can't insert objects in the editing context at their creation but only at the end of the transaction because the user can cancel his action at any time. Doing as decribed works (I do it myself) but if I do so, I will need to call at editingContext.revert() to prevent unwanted changes to be saved in the database by another part of my app using the same context. To sum up, I'm just looking for a method like EOEditingContext.awakeFromInsert except that it would be called before inserting objects instead of after. This way I could insert relationship members in the editingContext for each EOCustomObject in their own classes and avoid huge code blocks of insert in my main controller Regards FJ ___ Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list (Webobjects-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/webobjects-dev/award% 40apple.com This email sent to [EMAIL PROTECTED] ___ Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list (Webobjects-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/webobjects-dev/kenlists% 40anderhome.com This email sent to [EMAIL PROTECTED] ___ Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list (Webobjects-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: InsertObject
Hi, Frederic, I agree with all who are recommending the WO way of inserting your EOs into an editing context as soon as it's created. However, this can seem a bit daunting if you haven't been using peer or child editing contexts before, given the number of messages on the list about not properly locking the editing contexts that you create. The general problem has been solved, however, in two ways: * Project Wonder's ERXEC * Jonathan Rochkind's MultiECLockManager If you want deep background on the problem and its solutions, take a look at: http://en.wikibooks.org/wiki/Programming:WebObjects/EOF/ Using_EOF/Context_and_Database_Locking However, if you just want to solve your immediate problem and haven't yet bought into Project Wonder (which you should probably do soon), then I would suggest peer editing contexts, MultiECLockManager and would offer you the following notes: === // ** Add the MultiECLockManager class to your project from the following site http://www.wocode.com/WOCode/Files/MultiECLockManager.java // ** Add the following code to Session: // Add the following import statements: import com.webobjects.eoaccess.*; import java.lang.*; import java.util.*; // Define this ivar: private MultiECLockManager meclm; // Create a new lock manager in the Session initialization after super(); setLockManager(new MultiECLockManager()); // Add these methods to Session: public MultiECLockManager lockManager() { return meclm; } public void setLockManager(MultiECLockManager newLockManager) { meclm = newLockManager; } public void awake() { super.awake(); lockManager().lock(); } public void sleep() { if ( lockManager() != null ) lockManager().unlock(); super.sleep(); } public void terminate() { lockManager().unlock(); meclm = null; super.terminate(); } // ** Add the following code to a WOComponent that creates its own ec: // Define the EC ivar: private EOEditingContext ec; // Create register the new EC in the component's initialization method after super(context); ec = new EOEditingContext(); ((Session)session()).lockManager().registerEditingContext(ec); // Be sure to pass your newly created EC to any subsequent WOComponents needed for a multipage transaction. === Create a new editing context in any WOComponent that initiates a new logical transaction for the user. Do your ec.saveChanges() in some method of that component triggered by a Save button on the page. If the transaction requires more than a single page, pass your new editing context to each page in succession until the last page does the ec.saveChanges(). These notes don't represent the only way to implement MultiECLockManager, but they work well for the general case and if you use them initially, you can refine your approaches over time as more esoteric cases appear. As others have said, if your user starts a transaction and goes away before finishing it and doing the ec.saveChanges(), all of the EOs that they've created, all of the changes that they've wrought on that resulting object graph, and all the delete's that they've incurred are simply forgotten with no effect on your object store and no effect on your other editing contexts. Very handy. Regards, Jerry On May 30, 2007, at 7:20 AM, Frederic JECKER wrote: Hi, In my case, I can't insert objects in the editing context at their creation but only at the end of the transaction because the user can cancel his action at any time. Doing as decribed works (I do it myself) but if I do so, I will need to call at editingContext.revert() to prevent unwanted changes to be saved in the database by another part of my app using the same context. To sum up, I'm just looking for a method like EOEditingContext.awakeFromInsert except that it would be called before inserting objects instead of after. This way I could insert relationship members in the editingContext for each EOCustomObject in their own classes and avoid huge code blocks of insert in my main controller Regards FJ ___ Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list (Webobjects-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/webobjects-dev/jerrywwalker% 40gmail.com This email sent to [EMAIL PROTECTED] -- __ Jerry W. Walker, WebObjects Developer/Instructor for High Performance Industrial Strength Internet Enabled Systems [EMAIL PROTECTED] 203 278-4085office ___ Do not post admin requests to the list. They will be ignored.
Re: InsertObject
I might have missed something, but for his case I'm not sure he needs anything more than the default editing context, and since he's new to some of the concepts, that might be easier. Frederic, I usually just use the EOUtilities.createAndInsertInstance method to take care of both creation and inserting into the editing context at the same time. Using session's defaultEditingContext, locking and such are handled for you. And you can still revert() to undo changes, without saving them to the DB, back to the previous saveChanges(). Now, there are some advantages to creating a peer or child editing context and being able to dispose of the whole thing. I'm just saying I think this will work for you, and you can investigate the other options as you go forward. Regards, Mark On May 30, 2007, at 8:26 AM, Jerry W. Walker wrote: Hi, Frederic, I agree with all who are recommending the WO way of inserting your EOs into an editing context as soon as it's created. However, this can seem a bit daunting if you haven't been using peer or child editing contexts before, given the number of messages on the list about not properly locking the editing contexts that you create. The general problem has been solved, however, in two ways: * Project Wonder's ERXEC * Jonathan Rochkind's MultiECLockManager If you want deep background on the problem and its solutions, take a look at: http://en.wikibooks.org/wiki/Programming:WebObjects/EOF/ Using_EOF/Context_and_Database_Locking However, if you just want to solve your immediate problem and haven't yet bought into Project Wonder (which you should probably do soon), then I would suggest peer editing contexts, MultiECLockManager and would offer you the following notes: === // ** Add the MultiECLockManager class to your project from the following site http://www.wocode.com/WOCode/Files/MultiECLockManager.java // ** Add the following code to Session: // Add the following import statements: import com.webobjects.eoaccess.*; import java.lang.*; import java.util.*; // Define this ivar: private MultiECLockManager meclm; // Create a new lock manager in the Session initialization after super(); setLockManager(new MultiECLockManager()); // Add these methods to Session: public MultiECLockManager lockManager() { return meclm; } public void setLockManager(MultiECLockManager newLockManager) { meclm = newLockManager; } public void awake() { super.awake(); lockManager().lock(); } public void sleep() { if ( lockManager() != null ) lockManager().unlock(); super.sleep(); } public void terminate() { lockManager().unlock(); meclm = null; super.terminate(); } // ** Add the following code to a WOComponent that creates its own ec: // Define the EC ivar: private EOEditingContext ec; // Create register the new EC in the component's initialization method after super(context); ec = new EOEditingContext(); ((Session)session()).lockManager().registerEditingContext(ec); // Be sure to pass your newly created EC to any subsequent WOComponents needed for a multipage transaction. === Create a new editing context in any WOComponent that initiates a new logical transaction for the user. Do your ec.saveChanges() in some method of that component triggered by a Save button on the page. If the transaction requires more than a single page, pass your new editing context to each page in succession until the last page does the ec.saveChanges(). These notes don't represent the only way to implement MultiECLockManager, but they work well for the general case and if you use them initially, you can refine your approaches over time as more esoteric cases appear. As others have said, if your user starts a transaction and goes away before finishing it and doing the ec.saveChanges(), all of the EOs that they've created, all of the changes that they've wrought on that resulting object graph, and all the delete's that they've incurred are simply forgotten with no effect on your object store and no effect on your other editing contexts. Very handy. Regards, Jerry On May 30, 2007, at 7:20 AM, Frederic JECKER wrote: Hi, In my case, I can't insert objects in the editing context at their creation but only at the end of the transaction because the user can cancel his action at any time. Doing as decribed works (I do it myself) but if I do so, I will need to call at editingContext.revert() to prevent unwanted changes to be saved in the database by another part of my app using the same context. To sum up, I'm just looking for a method like EOEditingContext.awakeFromInsert except that it would be called before
Re: InsertObject
On 30.05.2007, at 09:49, Mark Morris wrote: I might have missed something, but for his case I'm not sure he needs anything more than the default editing context, and since he's new to some of the concepts, that might be easier. This is never really easier because it is s simple to screw up the default editing context and then you can't save anymore and you don't know why because errors happen in completely unrelated parts of the object graph. Just get used to different editing contexts. It makes your life so much easier! Applications that use only the defaultEditingContext scare me. cug ___ Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list (Webobjects-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: InsertObject
I personally stay away from using nested editing contexts, because I have been lost days of development a couple of times now by the state of the database context being mangled inexplicably when using a nested editing context. However, the exact same code works fine whenever I use a peer editing context rather than a nested editing context. No one posting to the list seems to have had these types of problems, however, so it might be specific to something else I was doing, but there is definitely some set of circumstances under which nested editing contexts do not work as advertised (and in a very frustrating, misleading way). At any rate, if you don't need any of the functionality you get from a nested editing context, I would avoid it and just create new (peer) editing contexts as needed. On May 30, 2007, at 11:56 AM, Guido Neitzer wrote: On 30.05.2007, at 09:49, Mark Morris wrote: I might have missed something, but for his case I'm not sure he needs anything more than the default editing context, and since he's new to some of the concepts, that might be easier. This is never really easier because it is s simple to screw up the default editing context and then you can't save anymore and you don't know why because errors happen in completely unrelated parts of the object graph. Just get used to different editing contexts. It makes your life so much easier! Applications that use only the defaultEditingContext scare me. cug ___ Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list (Webobjects-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/webobjects-dev/mark% 40bluecollarsoftware.com This email sent to [EMAIL PROTECTED] ___ Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list (Webobjects-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
insertObject error
Heylo,I am trying to create a form that will add the values to my database. The table's primary key, aid, is an auto increment column. Whenever I fill all of the values in the form and hit submit to add the data to the database, I get the following error:Application: OnTrackError: java.lang.IllegalArgumentException: attempt to insert a null object into this contextReason: attempt to insert a null object into this context I have checked to make sure that I am not leaving out any fields that are not null. As a matter of fact, the only field I am leaving out is the aid field. Here is my data for the submit button:public WOComponent addAnnouncement() { EOEditingContext ec = session().defaultEditingContext(); ec.insertObject(newAnnouncement); ec.saveChanges(); newAnnouncement = new Announcement(); return null; }I am confused and tired. Any help is greatly appreciated.Peace,Vickie ___ Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list (Webobjects-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com This email sent to archive@mail-archive.com
Re: insertObject error
vjc wrote: Heylo, I am trying to create a form that will add the values to my database. The table's primary key, aid, is an auto increment column. Whenever I fill all of the values in the form and hit submit to add the data to the database, I get the following error: Application: OnTrack Error: java.lang.IllegalArgumentException: attempt to insert a null object into this context Reason: attempt to insert a null object into this context This is telling you that your EO is null. That is, you havent got an object - just a null reference. I have checked to make sure that I am not leaving out any fields that are not null. As a matter of fact, the only field I am leaving out is the aid field. Here is my data for the submit button: public WOComponent addAnnouncement() { EOEditingContext ec = session().defaultEditingContext(); ec.insertObject(newAnnouncement); ec.saveChanges(); newAnnouncement = new Announcement(); return null; } I am confused and tired. Any help is greatly appreciated. You need to create the Announcment object before you insert into the editingContext. Your reference newAnnouncment is null, that is you cannot insert a null into the editingContext. eg) public WOComponent addAnnouncement() { newAnnouncement = new Announcement(); EOEditingContext ec = session().defaultEditingContext(); ec.insertObject(newAnnouncement); ec.saveChanges(); return null; } regards, - shaun ___ Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list (Webobjects-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com This email sent to archive@mail-archive.com