This is a question that comes from my lack of understanding how a
parent child editing context works. Well I think thats the cause :)
I have a page which allows some to add a new external member to the
site if they aren't already listed. So they click the "Add new
external member button." I create a new member in a child editing
context of the current editing context ( so if they cancel I can just
forget about it), they fill in the details and hit the save button, I
then return them to the page they were one and the new person now
becomes available to be selected from the pop-up list of external
members.
The code for making a new person :
public void initatenewExternalPerson()
{
EOEditingContext editCon = new EOEditingContext(ec); //Where
ec is the default editing context
createdPerson = (External)
EOUtilities.createAndInsertInstance
(editCon,"External");
PersonName newName = (PersonName)
EOUtilities.createAndInsertInstance(editCon,"PersonName");
createdPerson().addObjectToBothSidesOfRelationshipWithKey
(newName,"personNames");
checkingExternalExists = false;
}
The code for the save :
public WOComponent saveNewExternalPerson()
{
//Probably some over kill here on all the validation stuff but I
wanted to make sure this wasn't the problem.
try {
createdPerson.validateForSave();
} catch (NSValidation.ValidationException exception) {
validationDic.setObjectForKey
(Boolean.TRUE,"saveValidationFailed");
validationDic.setObjectForKey(exception.getMessage
(),"saveValidationString");
}
try {
createdPerson.externalDetails().validateForSave();
} catch (NSValidation.ValidationException exception) {
validationDic.setObjectForKey
(Boolean.TRUE,"saveValidationFailed");
validationDic.setObjectForKey(exception.getMessage
(),"saveValidationString");
}
try {
createdPerson.activeName().validateForSave();
} catch (NSValidation.ValidationException exception) {
validationDic.setObjectForKey
(Boolean.TRUE,"saveValidationFailed");
validationDic.setObjectForKey(exception.getMessage
(),"saveValidationString");
}
if ( validationDic.count() == 0 ) {
createdPerson.editingContext().saveChanges();
}
return context().page();
}
Which must be working as the person appears in the pop-up list as I
wanted them to, but as its only a child editing context it only
registers the object with eparent it doesn't save these changes to
the database ( at least thats my understanding after reading Chucks
book ).
However when I then click on the main save button, I get the
following error :
Error:
java.lang.IllegalStateException: Cannot obtain globalId for an object
which is registered in an other than the databaseContext's active
editingContext, object: {values = {accessLevel = -1; filePath = ;
onPayroll = 0; publications = (); staffNumber = ; brandNewRecord = 0;
publicationsEditorOf = (); jchiefGrants = (); toBeShown = 1; title
= ; groupsCoOrdinates = (); email = ; jotherGrants = ();
academicGeneral = ; fileName = ; publicationsAuthorOf = (); url = ;
personNames = (">"); groupMembers = (); hasFile = 0; externalDetails
= ">"; institution = ; type = 4; }; this = ">"; }, databaseContext:
com.webobjects.eoaccess.EODatabaseContext@487471, object's
editingContext: com.webobjects.eocontrol.EOEditingContext@ecea5e,
databaseContext's active editingContext:
com.webobjects.eocontrol.EOEditingContext@f46941
Reason:
Cannot obtain globalId for an object which is registered in an other
than the databaseContext's active editingContext, object: {values =
{accessLevel = -1; filePath =
<com.webobjects.foundation.NSKeyValueCoding$Null>; onPayroll = 0;
publications = (); staffNumber =
<com.webobjects.foundation.NSKeyValueCoding$Null>; brandNewRecord =
0; publicationsEditorOf = (); jchiefGrants = (); toBeShown = 1; title
= <com.webobjects.foundation.NSKeyValueCoding$Null>;
groupsCoOrdinates = (); email =
<com.webobjects.foundation.NSKeyValueCoding$Null>; jotherGrants = ();
academicGeneral = <com.webobjects.foundation.NSKeyValueCoding$Null>;
fileName = <com.webobjects.foundation.NSKeyValueCoding$Null>;
publicationsAuthorOf = (); url =
<com.webobjects.foundation.NSKeyValueCoding$Null>; personNames =
("<PersonName dd08eb <EOTemporaryGlobalID: 0 0 -126 -126 72 73 0 0 -6
79 3 0 0 0 1 6 83 75 92 -121 13 -113 120 -104>>"); groupMembers = ();
hasFile = 0; externalDetails = "<ExternalDetails 8c53f9
<EOTemporaryGlobalID: 0 0 -126 -126 72 73 0 0 -6 79 2 0 0 0 1 6 83 75
92 -121 13 -113 120 -104>>"; institution =
<com.webobjects.foundation.NSKeyValueCoding$Null>; type = 4; }; this
= "<External cb03c4 <EOTemporaryGlobalID: 0 0 -126 -126 72 73 0 0 -6
79 1 0 0 0 1 6 83 75 92 -121 13 -113 120 -104>>"; }, databaseContext:
com.webobjects.eoaccess.EODatabaseContext@487471, object's
editingContext: com.webobjects.eocontrol.EOEditingContext@ecea5e,
databaseContext's active editingContext:
com.webobjects.eocontrol.EOEditingContext@f46941
So its saying that it can't save my new person object as its
registered in another editing context. Which I guess is true it was
created in the child context, but I would have thought this would
have worked, so that is I had saved the child editing context which
then passes its changes up to the parent, save the parent, passes the
changes onto the DB. But this doesn't seem to be the case.
Have I done something wrong or is my understanding flawed.
Thanks Very Much
Owen McKerrow
WebMaster, emlab
http://emlab.uow.edu.au