Java 从Google App Engine中的集合中删除未被持久化
我在中看到了一个类似的问题,实际上我并没有在我的持久性管理器上调用close()。然而,我现在调用close,但是我的对象更新没有被持久化。具体来说,我想从一个集合中删除一个元素,并保存那个较小的集合。以下是与持久性管理器相关的代码,它不会引发异常,但不会保存我的数据:Java 从Google App Engine中的集合中删除未被持久化,java,google-app-engine,jdo,Java,Google App Engine,Jdo,我在中看到了一个类似的问题,实际上我并没有在我的持久性管理器上调用close()。然而,我现在调用close,但是我的对象更新没有被持久化。具体来说,我想从一个集合中删除一个元素,并保存那个较小的集合。以下是与持久性管理器相关的代码,它不会引发异常,但不会保存我的数据: UserService userService = UserServiceFactory.getUserService(); User user = userService.getCurrentUser();
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());
}
}
@PersistenceCapable(identityType=identityType.APPLICATION)
类UserProfileInfo{
@持久的
私人账户;
public AccountInfo removeAccount(长id)引发异常{
迭代器it=accounts.Iterator();
StringBuilder sb=新的StringBuilder();
while(it.hasNext()){
AccountInfo acctInfo=it.next();
Long acctInfoId=acctInfo.getId();
如果(acctInfoId.等于(id)){
it.remove();
返回acctInfo;
}
某人加上(“”);
某人附加(附加物);
}
抛出新异常(“找不到id”+id+“已尝试”+sb.toString());
}
}
我原以为调试任何东西时要做的第一件事就是查看日志(调试级别)。它告诉您对象在不同点处的状态。那么调用makePersistent()时它处于什么状态呢?之后呢?当调用pm.close()时会发生什么情况?我原以为调试任何东西时要做的第一件事就是查看日志(调试级别)。它告诉您对象在不同点处的状态。那么调用makePersistent()时它处于什么状态呢?之后呢?当您调用pm.close()时会发生什么情况?…因此答案似乎是owned对象不能使用长主键。datanucleus增强器对我添加的另一个对象类型告诉了我这一点。我不知道为什么它跳过了我的AccountInfo对象的此警告
我将密钥切换为字符串,并更改注释以正确使用该字符串,现在我可以从集合中删除。因此,答案似乎是owned对象无法使用长主键。datanucleus增强器对我添加的另一个对象类型告诉了我这一点。我不知道为什么它跳过了我的AccountInfo对象的此警告
我将密钥切换为字符串,并更改注释以正确使用该字符串,现在我可以从集合中删除。现在我只添加了一个“已删除”字段,该字段已添加到查询中。在使用另一个类时,我确实看到了一个提示—拥有对象的主键不能是“Long”,我不确定为什么没有为我的AccountInfo对象显示该警告,因为它使用Long作为主键,并且它是拥有的。我将尝试将其更改为字符串,然后看看效果如何。目前,我刚刚添加了一个“已删除”字段,该字段已添加到我的查询中。在使用另一个类时,我确实看到了一个提示—拥有对象的主键不能是“Long”,我不确定为什么没有为我的AccountInfo对象显示该警告,因为它使用Long作为主键,并且它是拥有的。我会试着把它改成字符串,看看效果如何。