JDO 未获取集合成员字段

发布于 2024-08-16 04:14:41 字数 1183 浏览 6 评论 0原文

有一个类:

class Node implements Serializable
{
    private String name;

    public String getName { return name; }
    public void setName(String val){ name = val; }

    public Node(){}
}

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
class NodeBag implements Serializable
{
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    Long id;

    @Persistent(serialized="true")
    private ArrayList<Node> nodes = new ArrayList<Node>();

    public String getNodes { return nodes; }
    public void setNodes(ArrayList<Node> val){ nodes = val; }

    public NodeBag(){}
}

我可以用这个将它保存到数据库

PersistenceManager pm = PMF.getManager();
try
{
 pm.makePersistent(newBag);
}
finally
{
 pm.close();
}

但是当我加载它时

PersistenceManager pm = PMF.getManager();
Query q = pm.newQuery(NodeBag.class);
try
{
 List<NodeBag> pipelines = (List<NodeBag>)q.execute();
 return nodeBags; // nodeBags[0].nodes is always empty
}
finally
{
 q.closeAll();
}

Nodebag.nodes 总是空的!

我错过了什么吗?

提前致谢。

问候, 保罗

Have a class:

class Node implements Serializable
{
    private String name;

    public String getName { return name; }
    public void setName(String val){ name = val; }

    public Node(){}
}

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
class NodeBag implements Serializable
{
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    Long id;

    @Persistent(serialized="true")
    private ArrayList<Node> nodes = new ArrayList<Node>();

    public String getNodes { return nodes; }
    public void setNodes(ArrayList<Node> val){ nodes = val; }

    public NodeBag(){}
}

I can save it to the db with this

PersistenceManager pm = PMF.getManager();
try
{
 pm.makePersistent(newBag);
}
finally
{
 pm.close();
}

But when i load it back

PersistenceManager pm = PMF.getManager();
Query q = pm.newQuery(NodeBag.class);
try
{
 List<NodeBag> pipelines = (List<NodeBag>)q.execute();
 return nodeBags; // nodeBags[0].nodes is always empty
}
finally
{
 q.closeAll();
}

Nodebag.nodes is always empty!

Did i miss something?

Thanks in advance.

Regards,
Paul

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

混浊又暗下来 2024-08-23 04:14:41

在返回对象的调用中,您可以使用 FetchPlan 来指定要返回的 FetchGroup。有关 FetchGroup 选项的更多信息,请参阅 JDO 文档

您可以通过在 PersistenceManager 中指定要使用的 FetchGroup 来确保提取所有实体。修改后的代码如下所示:

PersistenceManager pm = PMF.getManager();
pm.getFetchPlan().setGroup(FetchGroup.ALL);
Query q = pm.newQuery(NodeBag.class);
try {
  List<NodeBag> pipelines = (List<NodeBag>)q.execute();
  return nodeBags; // nodeBags[0].nodes is always empty
} finally {
  q.closeAll();
}

In your call to return the objects you can use the FetchPlan to specify what FetchGroup to return. See JDO docs for more information on the FetchGroup options.

You can ensure that all the entities are fetched, by specifying in your PersistenceManager the FetchGroup to use. The modified code is shown below:

PersistenceManager pm = PMF.getManager();
pm.getFetchPlan().setGroup(FetchGroup.ALL);
Query q = pm.newQuery(NodeBag.class);
try {
  List<NodeBag> pipelines = (List<NodeBag>)q.execute();
  return nodeBags; // nodeBags[0].nodes is always empty
} finally {
  q.closeAll();
}
明媚殇 2024-08-23 04:14:41

我在让提取组工作时遇到了很大的困难。 QueryPersistenceManager 都有一个 getFetchPlan(),但只有 PersistenceManager 上的那个似乎可以工作。

另外,请确保您的对象可分离,并对结果使用 pm.detachCopyAll()

I had a heck of a time getting fetch groups to work. Both Query and PersistenceManager have a getFetchPlan(), but only the one on PersistenceManager seems to work.

Also, make sure you make your objects detachable and use pm.detachCopyAll() on the result.

披肩女神 2024-08-23 04:14:41

错过了将其放入获取计划中吗?根据 DataNucleus 文档和 JDO 规范,可能会在默认获取组中进行标记,或者访问该字段,或者放入自定义获取计划中。

Missed putting it in the fetch plan ? mark in default fetch group perhaps, or access the field, or put in a custom fetch plan, as per the DataNucleus docs and JDO spec.

≈。彩虹 2024-08-23 04:14:41

实际上,我还想通过将答案转换为 JSON 来返回答案。
我已经成功加载了子对象。我使用的技巧是分离。通过分离,所有内容都将被加载。

谢谢。

Actually, i wanted also the return the answer across the wire by converting it to JSON.
And i've managed to load the child objects. The trick i use is detach. By detaching, everything will be loaded.

Thanks.

情话难免假 2024-08-23 04:14:41

使用 Collection 而不是 List,只要它位于事务内,它就应该开始工作

Use Collection insted of List and it should start working as long as it is inside a transaction

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文