在过去可以正常工作的 java appengine 代码上出现一个奇怪的错误(除了数据存储中的数据没有任何变化)。
我正在尝试迭代查询结果并更改实体的一些属性。该查询确实返回了一组结果,但是,当我尝试访问列表中的第一个结果时,它会在尝试访问其任何属性(但它的键)时引发异常。这是一个例外:
org.datanucleus.state.JDOStateManagerImpl isLoaded: Exception thrown by StateManager.isLoaded
Could not retrieve entity of kind OnTheCan with key OnTheCan(3204258)
org.datanucleus.exceptions.NucleusObjectNotFoundException: Could not retrieve entity of kind OnTheCan with key OnTheCan(3204258)
at org.datanucleus.store.appengine.DatastoreExceptionTranslator.wrapEntityNotFoundException(DatastoreExceptionTranslator.java:60)
这是我的代码:
PersistenceManager pm = PMF.get().getPersistenceManager();
Query query = null;
List<OnTheCan> cans;
query = pm.newQuery("SELECT this FROM " + OnTheCan.class.getName() + " WHERE open == true ORDER BY onTheCanId ASC");
query.setRange(0, num);
cans = (List<OnTheCan>) query.execute();
for (OnTheCan c : cans)
{
System.err.println(c.getOnTheCanId()); // this works fine! getting the key works
c.setOpen(false); // failure here with the above exception
c.setAutoClosed(true);
c.setEndTime(new Date(c.getStartTime().getTime() + 600000/*10*60*1000*/));
}
pm.close();
代码在尝试执行 c.setOpen(false) 时引发异常 - 这是我第一次访问或设置不是键的属性。因此,我的数据存储中似乎有一个虚拟实体,密钥为 3204258。这个实体并不真正存在(从管理控制台查询数据存储),但由于某种原因,它被查询返回。我的数据存储可能处于不一致的状态吗?
我通过将它作为我的 for 循环中的第一行来管理以下解决方法。显然是一个丑陋的黑客:
if (c.getOnTheCanId() == 3204258) { 继续; }
有任何想法吗?