我在中看到了类似的问题
Problems while saving a pre-persisted object in Google App Engine (Java)
实际上,我并没有在持久性管理器上调用close()。但是,我现在正在调用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());
}
}