2009-11-05 59 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

相关问题