我编写了一个测试用例来测试Objectify管理的Object Store的完整性:
@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());
}
}
一切都很好,直到while循环。由于一些奇怪的原因,迭代器显示相同的记录(不知道它只是没有迭代或同一记录被创建两次)
答案 0 :(得分:0)
很难确切地知道这里发生了什么,但你应该做的一件事是创建一个实体,保存它,然后尝试改变它的id。实体实例保存在会话缓存中,因此您可能会混淆缓存中的内容。
没有理由像这样回收对象实例。你永远不应该重置实体实例的id。