使用 JDO 在 GAE-J 中分离整个对象图时出现问题
我正在尝试加载 User 的完整对象图,其中包含 牌组的集合,然后包含卡牌的集合,如 这样的: 用户:
@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()
{
}
}
每个 Deck 都有一组卡片,如下所示:
@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();
}
这也不起作用,而且我已经没有想法了......
I am trying to load the full object graph for User, which contains a
collection of decks, which then contains a collection of cards, as
such:
User:
@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()
{
}
}
Each Deck has a collection of Cards, as such:
@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;
}
And finally, each card:
@PersistenceCapable
public class Card {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key _ID;
@Persistent
private Text _Question;
@Persistent
private Text _Answer;
@Persistent
private Deck _Parent;
}
I am trying to retrieve and then detach the entire object graph. I
can see in the debugger that it loads fine, but then when I get to
detaching, I can't make anything beyond the User object load. (No
Decks, no Cards). At first I tried without a transaction to simply
"touch" all the fields on the attached object before detaching, but
that didn't help. Then I tried adding everything to the default fetch
group, but that just generated warnings about GAE not supporting
joins. I tried setting the fetch plan's max fetch depth to -1, but
that didn't do it. Finally, I tried using FetchGroups as you can see
above, and then retrieving with the following code:
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();
}
This also doesn't work, and I'm running out of ideas...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我确信阅读日志(调试级别)会告诉您更多信息,因为它肯定会告诉您何时分离事物。也许 GAE/J 不尊重分离时的延迟加载? DataNucleus 本身与所有其他数据存储都可以正常工作。
当覆盖所有现有组时为什么要调用 FetchPlan.setGroup() ? addGroup() 对我来说更有意义。
I'm sure reading of the log (Debug level) would tell you way more, since it certainly tells you when it is detaching things. Perhaps GAE/J is not respecting lazy loading at detach ? DataNucleus itself works fine, with all other datastores.
Why call FetchPlan.setGroup() when that overwrites all existing groups ? addGroup() makes more sense to me.