I wrote a test case to test the integrity of the Object Store managed by Objectify:
@Test
public void testCreateDuplicateWithDiffID() throws NullValueException, NotFoundException {
gift.setGiftedOn(Calendar.getInstance().getTime());
gEndPt.create(gift);
int countBefore2ndCreate = OfyController.count(Gift.class);
// reinitialize the instanceID of the gift...now it's "different"
gift.initID();
// got to make sure we have a new date
gift.setGiftedOn(Calendar.getInstance().getTime());
// attempt to "re-create" the same instance
gEndPt.create(gift);
int countAfterCreate = OfyController.count(Gift.class);
// check the count...make sure it changes after the two inserts
assertThat("New Gift was NOT created", countAfterCreate, is(2));
QueryResultIterator<Gift> iterator = OfyController.ofy().load().type(Gift.class).iterator();
while (iterator.hasNext()){
log.info(iterator.next().getInstID());
}
}
Everything is fine up to the while loop. For some strange reason, the iterator is displaying the same record (don't know it's just not iterating through or the same record was created twice)
It's hard to know exactly what's going on here but one thing you should never do is create an entity, save it, then try to change its id. Entity instances are saved in the session cache so you're probably confusing the hell out of the cache.
There is no reason to recycle object instances like this. You should never reset the id of an entity instance.