使用 objectify returns 不同的结果存储和加载实体
storing and loading entities with objectify returns different results
我正在学习使用 objectify 存储和加载数据。我写了以下实体 class:
@Entity
public class Contact {
public Contact(){}
@Id
public Long id;
public String name;
public Contact(String name) {
this.name = name;
}
}
为了测试,我写了一个方法来删除我之前保存的联系人类型的所有实体:
List<Key<Contact>> keys = ObjectifyService.ofy().load().type(Contact.class).keys().list();
ObjectifyService.ofy().delete().keys(keys).now();
在方法结束时,我再次创建相同类型的新实体:
Contact contact1 = new Contact("name1");
ObjectifyService.ofy().save().entity(contact1).now();
Contact contact2 = new Contact("name2");
ObjectifyService.ofy().save().entity(contact2).now();
Contact contact3 = new Contact("name3");
ObjectifyService.ofy().save().entity(contact3).now();
Contact contact4 = new Contact("name4");
ObjectifyService.ofy().save().entity(contact4).now();
Contact contact5 = new Contact("name5");
ObjectifyService.ofy().save().entity(contact5).now();
奇怪的是,每次我重新运行这段代码时,我都会得到不同数量的结果(从 3 到 5 个返回的联系人实体不等):
List<Contact> contacts = ofy // loading the last created Contact entities
.load()
.type(Contact.class)
.list();
size = contacts.size(); // size is sometimes 3, 4 or 5
所以我应该更改什么才能在我重新运行代码后始终恢复我保存的所有 5 个联系人实体?
我正在学习使用 objectify 存储和加载数据。我写了以下实体 class:
@Entity
public class Contact {
public Contact(){}
@Id
public Long id;
public String name;
public Contact(String name) {
this.name = name;
}
}
为了测试,我写了一个方法来删除我之前保存的联系人类型的所有实体:
List<Key<Contact>> keys = ObjectifyService.ofy().load().type(Contact.class).keys().list();
ObjectifyService.ofy().delete().keys(keys).now();
在方法结束时,我再次创建相同类型的新实体:
Contact contact1 = new Contact("name1");
ObjectifyService.ofy().save().entity(contact1).now();
Contact contact2 = new Contact("name2");
ObjectifyService.ofy().save().entity(contact2).now();
Contact contact3 = new Contact("name3");
ObjectifyService.ofy().save().entity(contact3).now();
Contact contact4 = new Contact("name4");
ObjectifyService.ofy().save().entity(contact4).now();
Contact contact5 = new Contact("name5");
ObjectifyService.ofy().save().entity(contact5).now();
奇怪的是,每次我重新运行这段代码时,我都会得到不同数量的结果(从 3 到 5 个返回的联系人实体不等):
List<Contact> contacts = ofy // loading the last created Contact entities
.load()
.type(Contact.class)
.list();
size = contacts.size(); // size is sometimes 3, 4 or 5
所以我应该更改什么才能在我重新运行代码后始终恢复我保存的所有 5 个联系人实体?