我最近向我的一个不能为空的实体添加了文本字段。我想为其设置一个默认值,以便我在添加该字段之前存储的所有实体都将填充一个空字符串。这可以用 JDO 实现吗?
最佳答案
是的,尽管并不像您预期的那么微不足道。
限制
- 威尔 time out if it takes more then 30 seconds , 除非您将其作为任务运行,其中 万一它会time out if it takes more then 10 minutes .
- 没有比这更聪明的方法了 之后需要更新的实体 你can't query on a property that doesn't exist .
解决方法
- 您需要查看 appengine-mapreduce project到 得到一个可以实现的实现 完成超过 10 分钟 挂钟时间。
- 未知。
代码
void updateNullBarField() {
final Text DEFAULT_BAR = new Text("bar");
PersistenceManagerFactory pmfInstance = JDOHelper
.getPersistenceManagerFactory("transactions-optional");
PersistenceManager pm = pmfInstance.getPersistenceManager();
Query query = pm.newQuery(Foo.class);
@SuppressWarnings("unchecked")
Collection<Foo> foos = pm.detachCopyAll((List<Foo>) query.execute());
for (Foo foo : foos) {
if (foo.bar == null) {
foo.bar = DEFAULT_BAR;
pm.detachCopy(pm.makePersistent(foo));
}
}
}
关于java - 如何为 Appengine 上的 JDO 实体中的字段指定默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4403698/