从 Google App Engine 中的集合中删除不会被持久化

发布于 2024-08-10 06:32:46 字数 1641 浏览 4 评论 0原文

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

I see a similar question in Problems while saving a pre-persisted object in Google App Engine (Java), and indeed I was not calling close() on my persistence manager. However, I am now calling close, but my object update is not being persisted. Specifically, I want to remove an element from a Set, and save that smaller set. Here is the persistence manager related code, that doesn't throw an exception, but doesn't save my data:

    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();

And here is the relevant part of the UserProfileInfo class:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柏拉图鍀咏恒 2024-08-17 06:32:46

所以看起来答案是拥有的对象不能使用长主键。 datanucleus 增强器告诉我这是我添加的另一种对象类型。我不确定为什么它跳过了我的 AccountInfo 对象的此警告。

我将密钥切换为字符串,并更改了注释以正确使用该字符串,现在我可以从集合中删除。

So it looks like the answer is owned objects cannot use a Long primary key. The datanucleus enhancer told me this for another object type I added. I'm not sure why it skipped this warning for my AccountInfo object.

I switched my key over to a String, and changed the annotations to use the string properly, and now I'm able to delete from the collection.

奢欲 2024-08-17 06:32:46

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

I'd have thought that the first thing to do when debugging anything would be to look at the log (DEBUG level). It tells you what states the objects are in at the different points. So what state is it in when you call makePersistent() ? and after ? and what happens when you call pm.close() ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文