我在中看到类似的问题 在 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());
  }
}
有帮助吗?

解决方案

所以它看起来像答案是拥有的对象不能使用长的主键。该DataNucleus将增强告诉我,这对我又增加了对象类型。我不知道为什么它跳过此警告我AccountInfo对象。

我打开我的钥匙交给一个字符串,并改变了注释正确使用字符串,而现在我可以从收藏中删除。

其他提示

我还以为是,要做的第一件事,当调试东西会看日志(DEBUG等级)。它告诉你什么状态的对象是在不同的点。那么,什么状态是当你调用makePersistent这个()?之后 ?会发生什么,当你调用pm.close()...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top