Re: Editing context not reflecting edits?
Re: Editing context not reflecting edits?
- Subject: Re: Editing context not reflecting edits?
- From: Benjamin Adair <email@hidden>
- Date: Tue, 28 Sep 2004 09:28:27 -0500
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()'.
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.
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 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();
// If it is a new group add it to the editing context
if( webGroupIsNew.booleanValue() ){
session().defaultEditingContext().insertObject( webGroup );
} 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() );
newGroup.setCreateDate( webGroup.createDate() );
newGroup.setGroupId( webGroup.groupId() );
session().defaultEditingContext().insertObject( newGroup );
// Add the users
// Originally I only had:
// newGroup.setUsers( webGroup.users() );
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:
This email sent to email@hidden