我在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