2009-11-05 54 views
1

我在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()); 
    } 
} 

回答

1

所以它看起來像答案是擁有對象不能使用長主鍵。數據核增強器告訴我這是另一個我添加的對象類型。我不知道爲什麼它跳過了我的AccountInfo對象的警告。

我把我的鑰匙切換到一個字符串,並更改註釋以正確使用字符串,現在我可以從集合中刪除。

0

我我認爲調試任何東西時要做的第一件事就是查看日誌(DEBUG級別)。它會告訴你在不同點上物體處於什麼狀態。那麼當你調用makePersistent()時它是什麼狀態?之後 ?當你打電話pm.close()會發生什麼...

+0

現在我只是添加了一個'已刪除'的字段,我已經添加到我的查詢中。我在使用另一個類時看到了一個提示 - 擁有對象的主鍵不能是'Long'我不確定爲什麼沒有爲我的AccountInfo對象顯示該警告,因爲它使用Long作爲主關鍵,它是擁有的。我會嘗試將其更改爲字符串,並看看如何。 – 2009-11-09 18:35:11

相關問題