在google app engine中从集合中删除未被持久化

toiithl6  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(308)

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

ecfdbz9o1#

我原以为调试任何东西时要做的第一件事就是查看日志(调试级别)。它告诉你对象在不同的点处于什么状态。那么调用makepersistent()时它处于什么状态?之后呢?当你调用pm.close()时会发生什么。。。

kxeu7u2r

kxeu7u2r2#

因此,答案似乎是所有者对象不能使用长主键。datanucleus增强器对我添加的另一个对象类型告诉我这一点。我不知道为什么它跳过了我的accountinfo对象的警告。
我将我的键切换到一个字符串,并更改注解以正确使用该字符串,现在我可以从集合中删除。

相关问题