【发布时间】:2011-02-13 18:42:47
【问题描述】:
我花了几个小时试图解决这个问题,尽管尝试了几个建议,但我似乎无法让我的子实体更新。我已经广泛地查看了 GAE 文档,并尝试将事物放入事务中,尝试将它们设为“拥有”对象并使其成为“默认获取组”的一部分。运行下面的虚拟“populateDatastore”方法后,这些对象正确地保存在数据存储中,我可以毫无问题地从数据存储中检索它们。当我进行更改时,这些更改不会保留,尽管我使用的是 setter 方法,因此 JDO 会获取更改。我不是 Java 专家,所以我可能正在做一些非常明显错误的事情,我没有看到它。
父对象
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class Parent {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private String invitationCode;
@Persistent(defaultFetchGroup = "true")
private Child child;
// additional ivars and getters and setters
}
子对象
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class Response {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent(mappedBy = "child")
private Parent parent;
// additional ivars and getters and setters
}
我有几种方法可以检索和保存对象。
public void persistParent(Parent p, boolean closeSession){
manager = PMF.get().getPersistenceManager();
manager.makePersistent(p);
if(closeSession) {
manager.close();
}
}
public Transaction beginTransaction() {
int retries = 3;
manager = PMF.get().getPersistenceManager();
Transaction tx = manager.currentTransaction();
try {
tx.begin();
}
catch(com.google.apphosting.api.ApiProxy.CapabilityDisabledException e) {
return null;
}
catch(Exception e) {
return null;
}
return tx;
}
public boolean endTransaction(Transaction tx) throws Exception {
int retries = 3;
try {
tx.commit();
}
catch(ConcurrentModificationException e) {
if (retries == 0) {
throw e;
}
--retries;
}
catch(com.google.apphosting.api.ApiProxy.CapabilityDisabledException e) {
if (retries == 0) {
throw e;
}
--retries;
}
catch(Exception e) {
if (retries == 0) {
throw e;
}
--retries;
}
finally {
if(tx.isActive()) {
tx.rollback();
}
}
manager.close();
return true;
}
public List<Parent> getParentWithID(String code, boolean closeSession) {
Query q = PMF.get().getPersistenceManager().newQuery(Parent.class);
q.setFilter("code == codeParam");
q.declareParameters("String invitationCodeParam");
List<Invitation> results = null;
try {
results = (List<Parent>) q.execute(code);
}
finally {
if(closeSession) {
q.closeAll();
}
}
return results;
}
最后,我有一个虚拟方法可以将一些数据放入数据存储区进行测试。我将几个孩子添加到孩子列表中,但为了简洁起见,我已经删除了很多冗余代码。我也持久化了几个父对象。
Child c = new Child(arg1, arg2, arg3);
c.setSomeIVarForChild(arg1);
p = new Parent(arg1, arg2, arg3);
p.getChildList().add(c);
rsvpDao.persistParent(p, true);
【问题讨论】:
标签: java google-app-engine google-cloud-datastore entity jdo