我在Problems while saving a pre-persisted object in Google App Engine (Java)中看到类似的问题,实际上我并没有在我的持久性管理器上调用close()。不过,我现在正在调用close,但我的对象更新没有被保存。具体来说,我想从一个Set中删除一个元素,并保存那个较小的集合。下面是相关的代码的持久性管理器,这并不抛出异常,但不保存我的数据:从Google App Engine中的集合中删除不会被持久
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
PersistenceManager pm = PMF.get().getPersistenceManager();
UserProfileInfo userProfile = pm.getObjectById(UserProfileInfo.class,user.getUserId());
int presize = userProfile.getAccounts().size();
AccountInfo ai = userProfile.removeAccount(id);
int postsize = userProfile.getAccounts().size();
UserProfileInfo committed = (UserProfileInfo)pm.makePersistent(userProfile);
int postcommitsize = committed.getAccounts().size();
pm.close();
这里是UserProfileInfo类的相关部分:
@PersistenceCapable(identityType = IdentityType.APPLICATION)
class UserProfileInfo {
@Persistent
private Set<AccountInfo> accounts;
public AccountInfo removeAccount(Long id) throws Exception {
Iterator<AccountInfo> it = accounts.iterator();
StringBuilder sb = new StringBuilder();
while(it.hasNext()) {
AccountInfo acctInfo = it.next();
Long acctInfoId = acctInfo.getId();
if(acctInfoId.equals(id)) {
it.remove();
return acctInfo;
}
sb.append(" ");
sb.append(acctInfoId);
}
throw new Exception("Cannot find id " + id + " Tried " + sb.toString());
}
}
现在我只是添加了一个'已删除'的字段,我已经添加到我的查询中。我在使用另一个类时看到了一个提示 - 拥有对象的主键不能是'Long'我不确定为什么没有为我的AccountInfo对象显示该警告,因为它使用Long作为主关键,它是拥有的。我会尝试将其更改为字符串,并看看如何。 – 2009-11-09 18:35:11