Reputation: 20890
I recently added Text field to one of my Entities that cannot be null. I'd like to set a default value for it, so that all of the Entities that I stored before I added the field will be populated with an empty string. Is this possible with JDO?
Upvotes: 3
Views: 583
Reputation: 10824
Yes, though not as trivially as you were probably expecting.
Limitations
Workarounds
Code
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));
}
}
}
Upvotes: 2