如何使用 Objectify .ids() 删除一组实体
How to delete a set of Entities using Objectify .ids()
我正在使用 Objectify 从 GAE 读取一组实体:
List<CountStore> myList = ofy().load().type(CountStore.class).list();
然后我过滤并删除 myList 的一些元素。我现在想删除剩余列表中的所有 ID。我认为正确的方法是使用 .ids():
ofy().delete().type(CountStore.class).ids(myList);
但这不起作用 - 它崩溃了(见下文),因为它期望 myList 是 Long 或 String。谁能建议删除一组实体的最佳方法?
谢谢!
java.lang.IllegalArgumentException: id 'com.xyz.abc.CountStore@53d71fff' must be String or Long
at com.googlecode.objectify.util.DatastoreUtils.createKey(DatastoreUtils.java:66)
at com.googlecode.objectify.util.DatastoreUtils.createKeys(DatastoreUtils.java:112)
at com.googlecode.objectify.impl.DeleteTypeImpl.ids(DeleteTypeImpl.java:91)
at com.xyz.abc.CounterServlet.doGet(CountrServlet.java:45)
...
感谢您指出只需要一个长 ID 数组。答案真的很简单,就是创建一个 ID 数组列表,因为我过滤了要删除的实体:
ArrayList<Long> idList = new ArrayList<Long>();
//Do the Filtering and add IDs to the idList array the delete them all
ofy().delete().type(CountStore.class).ids(idList).now();
我已经抽象出删除如下:
public void deleteEntities(List<BaseClass> entities){
try{
ofy().delete().entities(entities);
}catch (Exception ex) {
logger.error("Exception", ex);
}
}
然后转储派生的 class 个对象:
List<DerivedClass> params = receivedFromSomeOtherFunction();
List<BaseCLass> params2 = new ArrayList<BaseClass>();
params2.addAll(params);
然后调用deleteEntities如下:
deleteEntities(params2);
然而,我发现一个缺点,有时这个函数调用需要时间来删除所有元素,尽管函数调用 returns 立即可能是因为它异步删除对象。
我正在使用 Objectify 从 GAE 读取一组实体:
List<CountStore> myList = ofy().load().type(CountStore.class).list();
然后我过滤并删除 myList 的一些元素。我现在想删除剩余列表中的所有 ID。我认为正确的方法是使用 .ids():
ofy().delete().type(CountStore.class).ids(myList);
但这不起作用 - 它崩溃了(见下文),因为它期望 myList 是 Long 或 String。谁能建议删除一组实体的最佳方法?
谢谢!
java.lang.IllegalArgumentException: id 'com.xyz.abc.CountStore@53d71fff' must be String or Long
at com.googlecode.objectify.util.DatastoreUtils.createKey(DatastoreUtils.java:66)
at com.googlecode.objectify.util.DatastoreUtils.createKeys(DatastoreUtils.java:112)
at com.googlecode.objectify.impl.DeleteTypeImpl.ids(DeleteTypeImpl.java:91)
at com.xyz.abc.CounterServlet.doGet(CountrServlet.java:45)
...
感谢您指出只需要一个长 ID 数组。答案真的很简单,就是创建一个 ID 数组列表,因为我过滤了要删除的实体:
ArrayList<Long> idList = new ArrayList<Long>();
//Do the Filtering and add IDs to the idList array the delete them all
ofy().delete().type(CountStore.class).ids(idList).now();
我已经抽象出删除如下:
public void deleteEntities(List<BaseClass> entities){
try{
ofy().delete().entities(entities);
}catch (Exception ex) {
logger.error("Exception", ex);
}
}
然后转储派生的 class 个对象:
List<DerivedClass> params = receivedFromSomeOtherFunction();
List<BaseCLass> params2 = new ArrayList<BaseClass>();
params2.addAll(params);
然后调用deleteEntities如下:
deleteEntities(params2);
然而,我发现一个缺点,有时这个函数调用需要时间来删除所有元素,尽管函数调用 returns 立即可能是因为它异步删除对象。