我在Problems while save a pre-persisted object in Google App Engine (Java)中看到了一个类似的问题,实际上我没有在我的持久性管理器上调用 close() 。但是,我现在打电话关闭,但我的对象更新没有被持久化。具体来说,我想从集合中删除一个元素,并保存那个较小的集合。这是与持久性管理器相关的代码,它不会引发异常,但不会保存我的数据:
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());
}
}