2
我試圖加載用戶的全部的對象圖,它包含一個 收集甲板,然後包含的卡的集合,作爲 例如: 用戶:問題拆卸整個對象圖中GAE-J與JDO
@PersistenceCapable(detachable = "true")
@Inheritance(strategy = InheritanceStrategy.SUBCLASS_TABLE)
@FetchGroup(name = "decks", members = { @Persistent(name =
"_Decks") })
public abstract class User {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
protected Key _ID;
@Persistent
protected String _UniqueIdentifier;
@Persistent(mappedBy = "_Owner")
@Element(dependent = "true")
protected Set<Deck> _Decks;
protected User()
{
}
}
每個甲板上有卡的集合,因爲這樣的:
@PersistenceCapable(detachable = "true")
@FetchGroup(name = "cards", members = { @Persistent(name =
"_Cards") })
public class Deck {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key _ID;
@Persistent
String _Name;
@Persistent(mappedBy = "_Parent")
@Element(dependent = "true")
private Set<Card> _Cards = new HashSet<Card>();
@Persistent
private Set<String> _Tags = new HashSet<String>();
@Persistent
private User _Owner;
}
最後,每張卡:
@PersistenceCapable
public class Card {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key _ID;
@Persistent
private Text _Question;
@Persistent
private Text _Answer;
@Persistent
private Deck _Parent;
}
我想檢索,然後分離整個對象圖。我在 可以看到在調試器,它加載好,但然後當我到達 分離,我不能做任何超出用戶對象加載。 (沒有 甲板,沒有卡)。起初,我試圖沒有交易,只是 「分離」前觸摸附加對象上的所有字段,但 沒有幫助。然後,我嘗試將所有內容添加到默認提取 組,但剛剛生成了關於GAE不支持 聯合的警告。我試着將抓取計劃的最大抓取深度設置爲-1,但是 沒有這樣做。最後,我嘗試使用FetchGroups,你可以在上面看到 ,然後用下面的代碼檢索:
PersistenceManager pm = _pmf.getPersistenceManager();
pm.setDetachAllOnCommit(true);
pm.getFetchPlan().setGroup("decks");
pm.getFetchPlan().setGroup("cards");
Transaction tx = pm.currentTransaction();
Query query = null;
try {
tx.begin();
query = pm.newQuery(GoogleAccountsUser.class); //Subclass of User
query.setFilter("_UniqueIdentifier == TheUser");
query.declareParameters("String TheUser");
List<User> results = (List<User>)query.execute(ID); //ID = Supplied
parameter
//TODO: Test for more than one result and throw
if(results.size() == 0)
{
tx.commit();
return null;
}
else
{
User usr = (User)results.get(0);
//usr = pm.detachCopy(usr);
tx.commit();
return usr;
}
} finally {
query.closeAll();
if (tx.isActive())
{
tx.rollback();
}
pm.close();
}
這也不起作用,而我運行的想法......
將setGroup()更改爲addGroup()完全修復了它。謝謝!令人驚訝的是,我幾乎所有的東西都可以正確使用,而且一個小小的錯誤使我耗費數日時間......我猜想它會發生什麼,以致於不熟悉複雜框架。 – tempy 2010-03-09 15:12:39