我花了好幾個小時試圖完成這一項,我似乎無法讓我的子實體更新,儘管嘗試了幾個建議。我已經廣泛地查看了GAE文檔,並且試圖將事物放入事務中,嘗試使它們成爲「擁有」對象,並使它們成爲「默認獲取組」的一部分。在運行下面的虛擬「populateDatastore」方法之後,這些對象正確地保存在數據存儲中,並且我可以從數據存儲檢索它們而不會出現問題。當我進行更改時,這些更改不會被保留,雖然我正在使用setter方法,所以JDO會選擇更改。我不是Java專家,所以我可能會做一些非常明顯錯誤的事情,我不會看到它。使用DataNucleus更新Google App Engine中的子對象JDO
父對象
@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);
PMF僅僅是谷歌例如用於PersistenceManagerFactory的Java單例。我將它作爲一個靜態引用來保存,所以我知道我正在使用同一個引用。有趣的是,如果我想處理它(使用分離),那麼我必須使用不同的工作流來獲取實體,而不是僅僅需要它用於只讀訪問,並且如上所述註釋類將不夠這是JDO的一個線索。 – davidstites 2011-02-14 14:31:54