所以,我有一個單向的一對多關係,我想讓孩子們在有序列表中。由於它們已經具有「索引」屬性,因此我試圖按照http://code.google.com/appengine/docs/java/datastore/jdo/relationships.html上的建議並使用「列表排序」擴展來使用該索引屬性來確定孩子的順序,而不是使用自動生成的順序。使用DataNucleus的列表順序擴展導致空列表
不幸的是,只要我添加註釋它停止返回的孩子,只給我一個空的列表。
我重新用這個簡單的例子問題:
@PersistenceCapable(detachable = "true")
@FetchGroup(name = "parent.children", members = {@Persistent(name = "children")})
public class Parent {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
@Order(extensions = @Extension(vendorName="datanucleus", key="list-ordering", value="index ASC"))
private List<Child> children;
// getters/setters
}
@PersistenceCapable(detachable = "true")
public class Child {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private Integer index;
// getters/setters
}
DAO:
public void save(T entity) {
PersistenceManager pm = getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(entity);
tx.commit();
} finally {
if(tx.isActive())
tx.rollback();
pm.close();
}
}
public T get(Key key, String... fetchGroups) {
PersistenceManager pm = getPersistenceManager();
Transaction tx = pm.currentTransaction();
addFetchGroups(pm, fetchGroups);
try {
tx.begin();
pm.setDetachAllOnCommit(true);
T entity = (T) pm.getObjectById(entityClass, key);
tx.commit();
return entity;
} finally {
if(tx.isActive())
tx.rollback();
pm.close();
}
}
測試代碼:
Parent parent = new Parent();
Child child = new Child();
child.setIndex(10);
parent.getChildren().add(child);
mParentDao.save(parent);
Parent parent2 = mParentDao.get(parent.getKey(), "parent.children");
有什麼特別,我做錯了什麼?
[編輯]下面是相關聯的日誌輸出:
數據存儲區:把一種PARENT的實體與關鍵PARENT(無-ID-還)
數據存儲:與關鍵PARENT樣的孩子把實體(3 )/兒童(不-ID-還)
數據存儲:INDEX:10
數據存儲:承諾儲存庫交易:0
數據存儲:開始新的數據存儲事務中:1
數據存儲:獲取樣父實體鍵家長(3 )
Datastore.Retrieve:準備查詢親子(3)種子的所有孩子
數據存儲。收回:添加排序:索引ASCENDING
Datastore.Retrieve:查詢有0個結果。
數據存儲:承諾儲存庫交易:1
適合我。我正在使用GAE插件的v2。使用日誌來調試它,因爲它告訴你放入並獲取GAE數據存儲 – DataNucleus 2012-03-19 18:46:40
我添加了一些日誌輸出。看起來很好,基本上......實體被正確添加並出現在數據存儲中,但由於某種原因,在查詢兒童時他收到0結果。 – 2012-04-01 17:00:08
我有一個類似的問題困擾了我一段時間。有序列表必須謹慎使用,特別是如果您在提交之前更改了排序字段。解決方法:刪除訂單標籤,然後在列表中實施一個「比較器」。 – marcolopes 2012-10-21 03:06:47