I think, the parent ec doesn't know anything what's in the child ec. So according to your code, the employee records belong to child ec. ec.saveChanges() after nested.saveChanges() inside the inner loop has no effect as there are no changes in parent ec to save at this point. Try nestC.delete() after nested.saveChanges(). I this works, then I guess you need to refetch the relationships for c before you call c.delete().
I have a suspicion that something is wrong with the way nested ECs are currently working. The errors I see are the result of missing snapshots. The errors are very sporadic and hard to reproduce. I've managed to distill this down to a simple test case. If I drop this:
public void testNestedECs() {
try {
EOEditingContext ec = ERXEC.newEditingContext();
for(int i = 0; i < 100; i++) {
Company c = (Company) EOUtilities.createAndInsertInstance(ec, Company.ENTITY_NAME);
c.setName(UUID.randomUUID().toString());
ec.saveChanges();
EOEditingContext nested = ERXEC.newEditingContext(ec);
Company nestC = c.localInstanceIn(nested);
for(int j = 0; j < 10; j++) {
Employee e = (Employee) EOUtilities.createAndInsertInstance(nested, Employee.ENTITY_NAME);
e.setFirstName(UUID.randomUUID().toString());
e.setLastName(UUID.randomUUID().toString());
e.setManager(Boolean.FALSE);
e.addObjectToBothSidesOfRelationshipWithKey(nestC, Employee.COMPANY_KEY);
nested.saveChanges();
ec.saveChanges();
}
c.delete();
ec.saveChanges(); //Line 78
}
} catch (Exception e) {
e.printStackTrace();
Assert.fail(e.getMessage());
}
}
into ERXECTest.java, I get fairly reliable failures due to missing snapshots.
java.lang.IllegalStateException: recordDeleteForObject: com.webobjects.eoaccess.EODatabaseContext com.webobjects.eoaccess.EODatabaseContext@489bb457 failed to find a snapshot for EO with Global ID:_EOIntegralKeyGlobalID[Employee (java.lang.Integer)491] that has been deleted from er.extensions.eof.ERXECer.extensions.eof.ERXEC@44581ea2. Cannot delete an object that has not been fetched from the database
at com.webobjects.eoaccess.EODatabaseContext.recordDeleteForObject(EODatabaseContext.java:4732)
at com.webobjects.eoaccess.EODatabaseContext.recordChangesInEditingContext(EODatabaseContext.java:5890)
at com.webobjects.eocontrol.EOObjectStoreCoordinator.saveChangesInEditingContext(EOObjectStoreCoordinator.java:373)
at com.webobjects.eocontrol.EOEditingContext.saveChanges(EOEditingContext.java:3192)
at er.extensions.eof.ERXEC._saveChanges(ERXEC.java:1177)
at er.extensions.eof.ERXEC.saveChanges(ERXEC.java:1100)
at er.extensions.eof.ERXECTest.testNestedECs(ERXECTest.java:78)
If I drop the number of loops down to 10 instead of 100, I don't see the exceptions. Does anyone see an obvious problem with my test case or perhaps have ideas about what's going wrong before I begin digging? :-)
Ramsey