Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Editing context not reflecting edits?




On Sep 28, 2004, at 7:28 AM, Benjamin Adair wrote:

Thanks for the feedback guys

My model appears to be set up correctly. I have a flattened relationship between my two entities. In the custom EO java file for both there is an appropriate 'setUsers()' and 'setGroups()'.

Are there any other relationships between Group and User? Sometimes that can confuse EOF.


I'm still trying to get my mind around this.. Not having taken the WO2 EO class and a couple years out from even the WO1 class I am sure my recollection of how to work with EOs is a bit hazy. I assumed, when telling the context that I'm deleting a group, that it would automatically remove that group from both sides of that relationship for me. And before deleting I would take old group's users() and feed that into the setUsers() method on the new group object. -And that would be it.

removeObjectFromBothSidesOfRelationshipWithKey can also help.


Now I've just gone and implemented code to iterate through each object in that existing many to many and add it to both sides of the relationship with the new (and it's so ugly it can't be right) and remove it from the old and I'm getting a fun error:

java.lang.IllegalStateException: setDatabaseOperator: com.webobjects.eoaccess.EODatabaseOperation {_dbSnapshot = {groupId = "testgroup"; userId = "test2"; }; _entity = "WebUserGroup"; _newRow = {groupId = "testgroup"; userId = "test2"; }; _globalID = _EOVectorKeyGlobalID[WebUserGroup (java.lang.String)test2,(java.lang.String)testgroup]; _databaseOperator = "EODatabaseDeleteOperator"; } -- illegal attempt to set change databaseOperator from delete to insert

I've seen that before. It is an EOF bug related to deleting, IIRC. Which version of WO are you using? Do you have any validateForDelete methods? Are you using delete rules in the model?


I really have to be on the wrong track here.. this seems crazy that with WO I have to do this much work to create a new EO with the same values and relationships.

Bah.. I feel like such a noob now. Here's the code post revision:
// All the data was saved from the request without error
// validate it for saving
webGroup.validateForSave();

That seems an odd thing to do.


// If it is a new group add it to the editing context
if( webGroupIsNew.booleanValue() ){
	session().defaultEditingContext().insertObject( webGroup );
Dude. You are working with EO objects that have not been inserted into an EC. Bad. No wonder you are having problems. See
http://www.wodev.com/cgi-bin/WebObjects/WODev.woa/wa/Main? wikiPage=EOFCommandments


} else {
// If it is not new, we need to watch for an update to the PK
if( ! webGroup.groupId().equals( webGroup.previousPK() ) ){
// The previous, saved PK and the new do not match
// Create a new group based off the current values, insert it and delete the old
WebGroup newGroup = new WebGroup();
newGroup.setComment( webGroup.comment() );
=8-0 Noooooooo. Grin. 1. Create 2. Insert 3. Manipulate Or just use this and EOF will do the Right Thing:
WebGroup newGroup = (WebGroup) EOUtilities.createAndInsertInstance(session().defaultEditingContext(), "WebGroup");


		newGroup.setCreateDate( webGroup.createDate() );
		newGroup.setGroupId( webGroup.groupId() );
		session().defaultEditingContext().insertObject( newGroup );
See above.  This does not go here.


		
		// Add the users
		// Originally I only had:
		// newGroup.setUsers( webGroup.users() );

I rarely do things like that but, on my first cup of coffee, it seems like it would work. Try fixing the above problems and try again.


Chuck

NSArray theUsers = webGroup.users();
if( theUsers != null )
{
for( int i = 0; i < theUsers.count(); i++ )
{
// For each user, remove from the relationship with the old group and add to the new
WebUser aUser = (WebUser)(theUsers.objectAtIndex( i ));

newGroup.addObjectToBothSidesOfRelationshipWithKey
( aUser, "users" );
webGroup.removeObjectFromBothSidesOfRelationshipWithKey
( aUser, "users" );
}
}
session().defaultEditingContext().deleteObject( webGroup );
}
}


// Save the changes made in the editing context to the database
session().defaultEditingContext().saveChanges();

Thanks again,
Ben

--

Benjamin Adair

Central Office Database Programmer/Analyst
Cancer & Leukemia Group B
Phone: 773-702-6731
Fax: 312-345-0117
Email: email@hidden
Web: http://www.calgb.org/

On Sep 28, 2004, at 9:03 AM, Nathan Dumar wrote:

Owen (and maybe Benjamin too),

Sounds like you have relationships going in only one direction, in your model. If you use the control-click-drag method to make relationships, a reciprocal relationship is made automatically.

Check the .java file (created by EOModeler) for each entity. GroupClass.java should have a setUser(UserClass) method, and UserClass.java should have a setGroup(GroupClass.java) method.

Take care,
Nathan

On Sep 28, 2004, at 12:49 AM, Owen McKerrow wrote:

I sometimes have the same problems even when I use addObjectToBothSidesOfRelationshipWithKey. WhatIve found however if say I saying

aUser.addObjectToBothSidesOfRelationshipWithKey(aGroup,"groups");

and changed that to

aGroup.addObjectToBothSidesOfRelationshipWithKey(aUser,"users");

it works fine. Something to do with which object you add the relationship to.

Mind you if you log out and then come back in ( i.e. get a new session ) it works fine anyways. Seems that for some reason ( may be my bad model ) but setting it one way makes all the right entries in the Db but the other Java Object doesn't get updated.

I hope that makes some kind of sense.

owen
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/chill%40global- village.net


This email sent to email@hidden
--
Practical WebObjects - a book for intermediate WebObjects developers who want to increase their overall knowledge of WebObjects, or those who are trying to solve specific application development problems.
http://www.global-village.net/products/practical_webobjects




_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/email@hidden

This email sent to email@hidden
References: 
 >Updating primary key value (From: Benjamin Adair <email@hidden>)
 >Editing context not reflecting edits? (From: Benjamin Adair <email@hidden>)
 >Re: Editing context not reflecting edits? (From: Chuck Hill <email@hidden>)
 >Re: Editing context not reflecting edits? (From: Owen McKerrow <email@hidden>)
 >Re: Editing context not reflecting edits? (From: Benjamin Adair <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.