代码之家  ›  专栏  ›  技术社区  ›  Michael Donohue Reno

从google app engine中的集合中删除不会被持久化

  •  1
  • Michael Donohue Reno  · 技术社区  · 15 年前

    我在中看到了类似的问题 Problems while saving a pre-persisted object in 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());
      }
    }
    
    2 回复  |  直到 15 年前
        1
  •  1
  •   Michael Donohue Reno    15 年前

    所以看起来答案是所有者对象不能使用长主键。对于我添加的另一个对象类型,DataNucleus增强器告诉了我这一点。我不知道为什么它跳过了我的accountinfo对象的这个警告。

    我将我的键切换到一个字符串,并更改注释以正确使用该字符串,现在我可以从集合中删除。

        2
  •  0
  •   DataNucleus    15 年前

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